Rewrite Fl_lock.cxx under the driver model.

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3-porting@11578 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Manolo Gouy
2016-04-10 20:38:04 +00:00
parent 95fa60b71d
commit 6879d54fc4
6 changed files with 98 additions and 26 deletions
+5
View File
@@ -152,6 +152,11 @@ public:
virtual void png_extra_rgba_processing(unsigned char *array, int w, int h) {}
// the default implementation is most probably enough
virtual const char *next_dir_sep(const char *start) { return strchr(start, '/');}
// implement to support threading
virtual void awake(void*) {}
virtual int lock() {return 1;}
virtual void unlock() {}
virtual void* thread_message() {return NULL;}
};
#endif // FL_SYSTEM_DRIVER_H
+75 -25
View File
@@ -16,12 +16,19 @@
// http://www.fltk.org/str.php
//
#include "config_lib.h"
#include <FL/Fl.H>
#include <config.h>
#include <stdlib.h>
#if defined(FL_CFG_GFX_QUARTZ)
#include "drivers/Darwin/Fl_Darwin_System_Driver.H"
#elif defined(FL_CFG_GFX_XLIB)
#include "drivers/Posix/Fl_Posix_System_Driver.H"
#elif defined(FL_CFG_SYS_WIN32)
#include "drivers/WinAPI/Fl_WinAPI_System_Driver.H"
#endif
/*
From Bill:
@@ -137,8 +144,6 @@ int Fl::awake(Fl_Awake_Handler func, void *data) {
return ret;
}
////////////////////////////////////////////////////////////////
// Windows threading...
/** \fn int Fl::lock()
The lock() method blocks the current thread until it
can safely access FLTK widgets and data. Child threads should
@@ -188,7 +193,9 @@ int Fl::awake(Fl_Awake_Handler func, void *data) {
See also: \ref advanced_multithreading
*/
#ifdef WIN32
#if defined(FL_CFG_SYS_WIN32)
////////////////////////////////////////////////////////////////
// Windows threading...
# include <windows.h>
# include <process.h>
# include <FL/x.H>
@@ -232,7 +239,7 @@ static void lock_function() {
EnterCriticalSection(&cs);
}
int Fl::lock() {
int Fl_WinAPI_System_Driver::lock() {
if (!main_thread) InitializeCriticalSection(&cs);
lock_function();
@@ -245,17 +252,18 @@ int Fl::lock() {
return 0;
}
void Fl::unlock() {
void Fl_WinAPI_System_Driver::unlock() {
unlock_function();
}
void Fl::awake(void* msg) {
void Fl_WinAPI_System_Driver::awake(void* msg) {
PostThreadMessage( main_thread, fl_wake_msg, (WPARAM)msg, 0);
}
#endif // FL_CFG_SYS_WIN32
////////////////////////////////////////////////////////////////
// POSIX threading...
#elif defined(HAVE_PTHREAD)
#if defined(HAVE_PTHREAD)
# include <unistd.h>
# include <fcntl.h>
# include <pthread.h>
@@ -306,12 +314,12 @@ static void unlock_function_rec() {
}
# endif // PTHREAD_MUTEX_RECURSIVE
void Fl::awake(void* msg) {
static void posix_awake(void* msg) {
if (write(thread_filedes[1], &msg, sizeof(void*))==0) { /* ignore */ }
}
static void* thread_message_;
void* Fl::thread_message() {
static void* posix_thread_message() {
void* r = thread_message_;
thread_message_ = 0;
return r;
@@ -332,7 +340,7 @@ static void thread_awake_cb(int fd, void*) {
extern void (*fl_lock_function)();
extern void (*fl_unlock_function)();
int Fl::lock() {
static int posix_lock() {
if (!thread_filedes[1]) {
// Initialize thread communication pipe to let threads awake FLTK
// from Fl::wait()
@@ -370,7 +378,7 @@ int Fl::lock() {
return 0;
}
void Fl::unlock() {
static void posix_unlock() {
fl_unlock_function();
}
@@ -389,33 +397,75 @@ void lock_ring() {
pthread_mutex_lock(ring_mutex);
}
#elif defined(FL_PORTING)
# pragma message "FL_PORTING: implement simple locking and unlocking for basic multithreading support"
#else
void unlock_ring() {
static void posix_awake(void*) {}
static int posix_lock() { return 1; }
static void posix_unlock() {}
static void* posix_thread_message() { return NULL; }
#endif // HAVE_PTHREAD
#if defined(FL_CFG_GFX_QUARTZ)
void Fl_Darwin_System_Driver::awake(void *v)
{
posix_awake(v);
}
void lock_ring() {
int Fl_Darwin_System_Driver::lock()
{
return posix_lock();
}
void Fl::awake(void*) {
void Fl_Darwin_System_Driver::unlock()
{
posix_unlock();
}
int Fl::lock() {
return 1;
void* Fl_Darwin_System_Driver::thread_message()
{
return posix_thread_message();
}
#endif // FL_CFG_GFX_QUARTZ
#if defined(FL_CFG_GFX_XLIB)
void Fl_Posix_System_Driver::awake(void *v)
{
posix_awake(v);
}
void Fl::unlock() {
int Fl_Posix_System_Driver::lock()
{
return posix_lock();
}
void Fl_Posix_System_Driver::unlock()
{
posix_unlock();
}
void* Fl_Posix_System_Driver::thread_message()
{
return posix_thread_message();
}
#endif // FL_CFG_GFX_XLIB
void Fl::awake(void *v) {
Fl::system_driver()->awake(v);
}
void* Fl::thread_message() {
return NULL;
return Fl::system_driver()->thread_message();
}
#endif // WIN32
int Fl::lock() {
return Fl::system_driver()->lock();
}
void Fl::unlock() {
Fl::system_driver()->unlock();
}
//
// End of "$Id$".
+2 -1
View File
@@ -54,6 +54,7 @@ void fl_cleanup_dc_list(void);
#include <FL/Fl_Screen_Driver.H>
#include <FL/Fl_Graphics_Driver.H> // for fl_graphics_driver
#include "drivers/WinAPI/Fl_WinAPI_Window_Driver.H"
#include "drivers/WinAPI/Fl_WinAPI_System_Driver.H"
#include <FL/fl_utf8.h>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
@@ -371,7 +372,7 @@ void (*fl_lock_function)() = nothing;
void (*fl_unlock_function)() = nothing;
static void* thread_message_;
void* Fl::thread_message() {
void* Fl_WinAPI_System_Driver::thread_message() {
void* r = thread_message_;
thread_message_ = 0;
return r;
@@ -80,6 +80,11 @@ public:
virtual char *preference_rootnode(Fl_Preferences *prefs, Fl_Preferences::Root root, const char *vendor,
const char *application);
virtual void *dlopen(const char *filename);
// these 4 are implemented in Fl_lock.cxx
virtual void awake(void*);
virtual int lock();
virtual void unlock();
virtual void* thread_message();
};
#endif // FL_DARWIN_SYSTEM_DRIVER_H
@@ -81,6 +81,11 @@ public:
const char *application);
virtual int preferences_need_protection_check() {return 1;}
virtual void *dlopen(const char *filename);
// these 4 are implemented in Fl_lock.cxx
virtual void awake(void*);
virtual int lock();
virtual void unlock();
virtual void* thread_message();
};
#endif // FL_POSIX_SYSTEM_DRIVER_H
@@ -86,6 +86,12 @@ public:
virtual void *dlopen(const char *filename);
virtual void png_extra_rgba_processing(unsigned char *array, int w, int h);
virtual const char *next_dir_sep(const char *start);
// these 3 are implemented in Fl_lock.cxx
virtual void awake(void*);
virtual int lock();
virtual void unlock();
// this one is implemented in Fl_win32.cxx
virtual void* thread_message();
};
#endif // FL_WINAPI_SYSTEM_DRIVER_H