2 w401229755 w401229755 于 2014.08.11 11:26 提问

Fence?android中的东西 你们有听说过吗?进来一起讨论下

fence,看过源代码的人肯定不会陌生,中文是栅栏的意思,用在android里好像是为了解决cpu调度时乱序的问题,也就是串行操作?? 我不清楚,有大神出来一起讨论下吗,小弟在研究surfaceflinger,里面太多fence 至今没搞清楚它的机制。。。。。而且网上一点资料都没有!!!让这个问答成为第一份资料吧,日后要是我研究有所结果定会分享!!

下面贴出它类的定义 :
在frameworks/native/include/ui 下

class Fence
: public LightRefBase, public Flattenable
static const sp NO_FENCE;

// TIMEOUT_NEVER may be passed to the wait method to indicate that it
// should wait indefinitely for the fence to signal.
enum { TIMEOUT_NEVER = -1 };

// Construct a new Fence object with an invalid file descriptor.  This
// should be done when the Fence object will be set up by unflattening
// serialized data.

// Construct a new Fence object to manage a given fence file descriptor.
// When the new Fence object is destructed the file descriptor will be
// closed.
Fence(int fenceFd);

// Check whether the Fence has an open fence file descriptor. Most Fence
// methods treat an invalid file descriptor just like a valid fence that
// is already signalled, so using this is usually not necessary.
bool isValid() const { return mFenceFd != -1; }

// wait waits for up to timeout milliseconds for the fence to signal.  If
// the fence signals then NO_ERROR is returned. If the timeout expires
// before the fence signals then -ETIME is returned.  A timeout of
// TIMEOUT_NEVER may be used to indicate that the call should wait
// indefinitely for the fence to signal.
status_t wait(unsigned int timeout);

// waitForever is a convenience function for waiting forever for a fence to
// signal (just like wait(TIMEOUT_NEVER)), but issuing an error to the
// system log and fence state to the kernel log if the wait lasts longer
// than a warning timeout.
// The logname argument should be a string identifying
// the caller and will be included in the log message.
status_t waitForever(const char* logname);

// merge combines two Fence objects, creating a new Fence object that
// becomes signaled when both f1 and f2 are signaled (even if f1 or f2 is
// destroyed before it becomes signaled).  The name argument specifies the
// human-readable name to associated with the new Fence object.
static sp<Fence> merge(const String8& name, const sp<Fence>& f1,
        const sp<Fence>& f2);

// Return a duplicate of the fence file descriptor. The caller is
// responsible for closing the returned file descriptor. On error, -1 will
// be returned and errno will indicate the problem.
int dup() const;

// getSignalTime returns the system monotonic clock time at which the
// fence transitioned to the signaled state.  If the fence is not signaled
// then INT64_MAX is returned.  If the fence is invalid or if an error
// occurs then -1 is returned.
nsecs_t getSignalTime() const;

// Flattenable interface
size_t getFlattenedSize() const;
size_t getFdCount() const;
status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);

// Only allow instantiation using ref counting.
friend class LightRefBase;

// Disallow copying
Fence(const Fence& rhs);
Fence& operator = (const Fence& rhs);
const Fence& operator = (const Fence& rhs) const;

int mFenceFd;


Csdn user default icon