Some improvements as discussed. Not a final feature! May be removed without notice!

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@5732 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Matthias Melcher 2007-03-06 19:48:45 +00:00
parent c4ad147d5f
commit 4102380d65

View File

@ -77,10 +77,14 @@ int Fl::awake_ring_head_;
int Fl::awake_ring_tail_; int Fl::awake_ring_tail_;
const int AWAKE_RING_SIZE = 1024; const int AWAKE_RING_SIZE = 1024;
static void lock_ring();
static void unlock_ring();
int Fl::add_awake_handler_(Fl_Awake_Handler func, void *data) int Fl::add_awake_handler_(Fl_Awake_Handler func, void *data)
{ {
int ret = 0; int ret = 0;
Fl::lock(); lock_ring();
if (!awake_ring_) { if (!awake_ring_) {
awake_ring_size_ = AWAKE_RING_SIZE; awake_ring_size_ = AWAKE_RING_SIZE;
awake_ring_ = (Fl_Awake_Handler*)malloc(awake_ring_size_*sizeof(Fl_Awake_Handler)); awake_ring_ = (Fl_Awake_Handler*)malloc(awake_ring_size_*sizeof(Fl_Awake_Handler));
@ -96,24 +100,25 @@ int Fl::add_awake_handler_(Fl_Awake_Handler func, void *data)
if (awake_ring_head_ == awake_ring_size_) if (awake_ring_head_ == awake_ring_size_)
awake_ring_head_ = 0; awake_ring_head_ = 0;
} }
Fl::unlock(); unlock_ring();
return ret; return ret;
} }
int Fl::get_awake_handler_(Fl_Awake_Handler &func, void *&data) int Fl::get_awake_handler_(Fl_Awake_Handler &func, void *&data)
{ {
// this function must only be called from within the event int ret = 0;
// loop which is locked, so don't bother creating any locks lock_ring();
if (!awake_ring_) if (!awake_ring_ || awake_ring_head_ == awake_ring_tail_) {
return -1; ret = -1;
if (awake_ring_head_ == awake_ring_tail_) } else {
return -1;
func = awake_ring_[awake_ring_tail_]; func = awake_ring_[awake_ring_tail_];
data = awake_data_[awake_ring_tail_]; data = awake_data_[awake_ring_tail_];
++awake_ring_tail_; ++awake_ring_tail_;
if (awake_ring_tail_ == awake_ring_size_) if (awake_ring_tail_ == awake_ring_size_)
awake_ring_tail_ = 0; awake_ring_tail_ = 0;
return 0; }
unlock_ring();
return ret;
} }
// //
@ -142,6 +147,19 @@ static DWORD main_thread;
// Microsoft's version of a MUTEX... // Microsoft's version of a MUTEX...
CRITICAL_SECTION cs; CRITICAL_SECTION cs;
CRITICAL_SECTION *cs_ring;
void unlock_ring() {
LeaveCriticalSection(cs_ring);
}
void lock_ring() {
if (!cs_ring) {
cs_ring = (CRITICAL_SECTION*)malloc(sizeof(CRITICAL_SECTION));
InitializeCriticalSection(cs_ring);
}
EnterCriticalSection(cs_ring);
}
// //
// 'unlock_function()' - Release the lock. // 'unlock_function()' - Release the lock.
@ -204,7 +222,7 @@ void Fl::awake(void* msg) {
// Pipe for thread messaging via Fl::awake()... // Pipe for thread messaging via Fl::awake()...
static int thread_filedes[2]; static int thread_filedes[2];
// Mutux and state information for Fl::lock() and Fl::unlock()... // Mutex and state information for Fl::lock() and Fl::unlock()...
static pthread_mutex_t fltk_mutex; static pthread_mutex_t fltk_mutex;
static pthread_t owner; static pthread_t owner;
static int counter; static int counter;
@ -310,6 +328,23 @@ void Fl::lock() {
void Fl::unlock() { void Fl::unlock() {
fl_unlock_function(); fl_unlock_function();
} }
// Mutex code for the awake ring buffer
static pthread_mutex_t *ring_mutex;
void unlock_ring() {
pthread_mutex_unlock(ring_mutex);
}
void lock_ring() {
if (!ring_mutex) {
ring_mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(ring_mutex, NULL);
}
pthread_mutex_lock(ring_mutex);
}
#endif // WIN32 #endif // WIN32
// //