Merge branch 'master' into newmouse

ugfx_release_2.6
inmarket 2014-09-29 15:47:47 +10:00
commit 611133cbc0
5 changed files with 173 additions and 133 deletions

View File

@ -14,8 +14,6 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h> #include <time.h>
#if USE_SCHED_NOT_PTHREAD_YIELD #if USE_SCHED_NOT_PTHREAD_YIELD
#include <sched.h> #include <sched.h>
@ -70,16 +68,16 @@ void gfxSleepMilliseconds(delaytime_t ms) {
default: default:
ts.tv_sec = ms / 1000; ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000; ts.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&ts, 0); nanosleep(&ts, 0);
return; return;
} }
} }
void gfxSleepMicroseconds(delaytime_t ms) { void gfxSleepMicroseconds(delaytime_t us) {
struct timespec ts; struct timespec ts;
switch(ms) { switch(us) {
case TIME_IMMEDIATE: case TIME_IMMEDIATE:
linuxyield(); linuxyield();
return; return;
@ -90,8 +88,8 @@ void gfxSleepMicroseconds(delaytime_t ms) {
return; return;
default: default:
ts.tv_sec = ms / 1000000; ts.tv_sec = us / 1000000;
ts.tv_nsec = ms % 1000000; ts.tv_nsec = (us % 1000000) * 1000;
nanosleep(&ts, 0); nanosleep(&ts, 0);
return; return;
} }
@ -101,8 +99,7 @@ systemticks_t gfxSystemTicks(void) {
struct timespec ts; struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts); clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
return ts.tv_sec * 1000UL + ts.tv_nsec / 1000UL;
} }
gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param) { gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param) {
@ -133,81 +130,119 @@ threadreturn_t gfxThreadWait(gfxThreadHandle thread) {
return retval; return retval;
} }
void gfxSemInit(gfxSem *pSem, semcount_t val, semcount_t limit) { #if GFX_USE_POSIX_SEMAPHORES
pthread_mutex_init(&pSem->mtx, 0); void gfxSemInit(gfxSem *pSem, semcount_t val, semcount_t limit) {
pthread_cond_init(&pSem->cond, 0); pSem->max = limit;
pthread_mutex_lock(&pSem->mtx); sem_init(&pSem->sem, 0, val);
pSem->cnt = val; }
pSem->max = limit; void gfxSemDestroy(gfxSem *pSem) {
pthread_mutex_unlock(&pSem->mtx); sem_destroy(&pSem->sem);
} }
bool_t gfxSemWait(gfxSem *pSem, delaytime_t ms) {
void gfxSemDestroy(gfxSem *pSem) { switch (ms) {
pthread_mutex_destroy(&pSem->mtx);
pthread_cond_destroy(&pSem->cond);
}
bool_t gfxSemWait(gfxSem *pSem, delaytime_t ms) {
pthread_mutex_lock(&pSem->mtx);
switch (ms) {
case TIME_INFINITE: case TIME_INFINITE:
while (!pSem->cnt) return sem_wait(&pSem->sem) ? FALSE : TRUE;
pthread_cond_wait(&pSem->cond, &pSem->mtx);
break;
case TIME_IMMEDIATE: case TIME_IMMEDIATE:
if (!pSem->cnt) { return sem_trywait(&pSem->sem) ? FALSE : TRUE;
pthread_mutex_unlock(&pSem->mtx);
return FALSE;
}
break;
default: default:
{ {
struct timeval now;
struct timespec tm; struct timespec tm;
gettimeofday(&now, 0); clock_gettime(CLOCK_REALTIME, &tm);
tm.tv_sec = now.tv_sec + ms / 1000; tm.tv_sec += ms / 1000;
tm.tv_nsec = (now.tv_usec + ms % 1000) * 1000; tm.tv_nsec += (ms % 1000) * 1000000;
while (!pSem->cnt) { return sem_timedwait(&pSem->sem, &tm) ? FALSE : TRUE;
if (pthread_cond_timedwait(&pSem->cond, &pSem->mtx, &tm) == ETIMEDOUT) { }
pthread_mutex_unlock(&pSem->mtx); }
return FALSE; }
void gfxSemSignal(gfxSem *pSem) {
if (gfxSemCounter(pSem) < pSem->max)
sem_post(&pSem->sem);
}
semcount_t gfxSemCounter(gfxSem *pSem) {
int res;
res = 0;
sem_getvalue(&pSem->sem, &res);
return res;
}
#else
void gfxSemInit(gfxSem *pSem, semcount_t val, semcount_t limit) {
pthread_mutex_init(&pSem->mtx, 0);
pthread_cond_init(&pSem->cond, 0);
pthread_mutex_lock(&pSem->mtx);
pSem->cnt = val;
pSem->max = limit;
pthread_mutex_unlock(&pSem->mtx);
}
void gfxSemDestroy(gfxSem *pSem) {
pthread_mutex_destroy(&pSem->mtx);
pthread_cond_destroy(&pSem->cond);
}
bool_t gfxSemWait(gfxSem *pSem, delaytime_t ms) {
pthread_mutex_lock(&pSem->mtx);
switch (ms) {
case TIME_INFINITE:
while (!pSem->cnt)
pthread_cond_wait(&pSem->cond, &pSem->mtx);
break;
case TIME_IMMEDIATE:
if (!pSem->cnt) {
pthread_mutex_unlock(&pSem->mtx);
return FALSE;
}
break;
default:
{
struct timespec tm;
clock_gettime(CLOCK_REALTIME, &tm);
tm.tv_sec += ms / 1000;
tm.tv_nsec += (ms % 1000) * 1000000;
while (!pSem->cnt) {
// We used to test the return value for ETIMEDOUT. This doesn't
// work in some current pthread libraries which return -1 instead
// and set errno to ETIMEDOUT. So, we will return FALSE on any error
// including a ETIMEDOUT.
if (pthread_cond_timedwait(&pSem->cond, &pSem->mtx, &tm)) {
pthread_mutex_unlock(&pSem->mtx);
return FALSE;
}
} }
} }
} break;
break; }
pSem->cnt--;
pthread_mutex_unlock(&pSem->mtx);
return TRUE;
} }
void gfxSemSignal(gfxSem *pSem) {
pthread_mutex_lock(&pSem->mtx);
pSem->cnt--; if (pSem->cnt < pSem->max) {
pthread_mutex_unlock(&pSem->mtx); pSem->cnt++;
pthread_cond_signal(&pSem->cond);
}
return TRUE; pthread_mutex_unlock(&pSem->mtx);
}
void gfxSemSignal(gfxSem *pSem) {
pthread_mutex_lock(&pSem->mtx);
if (pSem->cnt < pSem->max) {
pSem->cnt++;
pthread_cond_signal(&pSem->cond);
} }
semcount_t gfxSemCounter(gfxSem *pSem) {
semcount_t res;
pthread_mutex_unlock(&pSem->mtx); // The locking is really only required if obtaining the count is a divisible operation
} // which it might be on a 8/16 bit processor with a 32 bit semaphore count.
pthread_mutex_lock(&pSem->mtx);
res = pSem->cnt;
pthread_mutex_unlock(&pSem->mtx);
semcount_t gfxSemCounter(gfxSem *pSem) { return res;
semcount_t res; }
#endif // GFX_USE_POSIX_SEMAPHORES
// The locking is really only required if obtaining the count is a divisible operation
// which it might be on a 8/16 bit processor with a 32 bit semaphore count.
pthread_mutex_lock(&pSem->mtx);
res = pSem->cnt;
pthread_mutex_unlock(&pSem->mtx);
return res;
}
#endif /* GFX_USE_OS_LINUX */ #endif /* GFX_USE_OS_LINUX */

View File

@ -10,11 +10,20 @@
#if GFX_USE_OS_LINUX #if GFX_USE_OS_LINUX
// We don't put this in the general sys_options.h as it is Linux specific.
#ifndef GFX_USE_POSIX_SEMAPHORES
#define GFX_USE_POSIX_SEMAPHORES TRUE
#endif
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <pthread.h> #include <pthread.h>
#if GFX_USE_POSIX_SEMAPHORES
#include <semaphore.h>
#endif
/* Already defined int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, size_t */ /* Already defined int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, size_t */
typedef int8_t bool_t; typedef int8_t bool_t;
@ -42,7 +51,6 @@ typedef pthread_mutex_t gfxMutex;
#define gfxMutexExit(pmtx) pthread_mutex_unlock(pmtx) #define gfxMutexExit(pmtx) pthread_mutex_unlock(pmtx)
#define gfxSemWaitI(psem) gfxSemWait(psem, TIME_IMMEDIATE) #define gfxSemWaitI(psem) gfxSemWait(psem, TIME_IMMEDIATE)
#define gfxSemSignalI(psem) gfxSemSignal(psem) #define gfxSemSignalI(psem) gfxSemSignal(psem)
#define gfxSemCounterI(pSem) ((pSem)->cnt)
#define TIME_IMMEDIATE 0 #define TIME_IMMEDIATE 0
#define TIME_INFINITE ((delaytime_t)-1) #define TIME_INFINITE ((delaytime_t)-1)
@ -51,12 +59,21 @@ typedef pthread_mutex_t gfxMutex;
#define NORMAL_PRIORITY 0 #define NORMAL_PRIORITY 0
#define HIGH_PRIORITY -10 #define HIGH_PRIORITY -10
typedef struct gfxSem { #if GFX_USE_POSIX_SEMAPHORES
pthread_mutex_t mtx; typedef struct gfxSem {
pthread_cond_t cond; sem_t sem;
semcount_t cnt; semcount_t max;
semcount_t max; } gfxSem;
} gfxSem; #define gfxSemCounterI(psem) gfxSemCounter(psem)
#else
typedef struct gfxSem {
pthread_mutex_t mtx;
pthread_cond_t cond;
semcount_t cnt;
semcount_t max;
} gfxSem;
#define gfxSemCounterI(psem) ((psem)->cnt)
#endif
/*===========================================================================*/ /*===========================================================================*/
/* Function declarations. */ /* Function declarations. */

View File

@ -21,18 +21,6 @@
static gfxMutex SystemMutex; static gfxMutex SystemMutex;
void get_ticks(mach_timespec_t *mts){
clock_serv_t cclock;
//mach_timespec_t mts;
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, mts);
mach_port_deallocate(mach_task_self(), cclock);
}
void _gosInit(void) void _gosInit(void)
{ {
/* No initialization of the operating system itself is needed */ /* No initialization of the operating system itself is needed */
@ -66,35 +54,35 @@ void gfxSleepMilliseconds(delaytime_t ms) {
case TIME_INFINITE: while(1) sleep(60); return; case TIME_INFINITE: while(1) sleep(60); return;
default: default:
ts.tv_sec = ms / 1000; ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000; ts.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&ts, 0); nanosleep(&ts, 0);
return; return;
} }
} }
void gfxSleepMicroseconds(delaytime_t ms) { void gfxSleepMicroseconds(delaytime_t us) {
struct timespec ts; struct timespec ts;
switch(ms) { switch(us) {
case TIME_IMMEDIATE: gfxYield(); return; case TIME_IMMEDIATE: gfxYield(); return;
case TIME_INFINITE: while(1) sleep(60); return; case TIME_INFINITE: while(1) sleep(60); return;
default: default:
ts.tv_sec = ms / 1000000; ts.tv_sec = us / 1000000;
ts.tv_nsec = ms % 1000000; ts.tv_nsec = (us % 1000000) * 1000;
nanosleep(&ts, 0); nanosleep(&ts, 0);
return; return;
} }
} }
systemticks_t gfxSystemTicks(void) { systemticks_t gfxSystemTicks(void) {
//struct timespec ts; mach_timespec_t ts;
//clock_gettime(CLOCK_MONOTONIC, &ts); clock_serv_t cclock;
mach_timespec_t ts; host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
get_ticks(&ts); clock_get_time(cclock, &ts);
mach_port_deallocate(mach_task_self(), cclock);
return ts.tv_sec * 1000UL + ts.tv_nsec / 1000000;
return ts.tv_sec * 1000UL + ts.tv_nsec / 1000UL;
} }
gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param) { gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param) {
@ -157,9 +145,13 @@ bool_t gfxSemWait(gfxSem *pSem, delaytime_t ms) {
gettimeofday(&now, 0); gettimeofday(&now, 0);
tm.tv_sec = now.tv_sec + ms / 1000; tm.tv_sec = now.tv_sec + ms / 1000;
tm.tv_nsec = (now.tv_usec + ms % 1000) * 1000; tm.tv_nsec = now.tv_usec * 1000 + (ms % 1000) * 1000000;
while (!pSem->cnt) { while (!pSem->cnt) {
if (pthread_cond_timedwait(&pSem->cond, &pSem->mtx, &tm) == ETIMEDOUT) { // We used to test the return value for ETIMEDOUT. This doesn't
// work in some current pthread libraries which return -1 instead
// and set errno to ETIMEDOUT. So, we will return FALSE on any error
// including a ETIMEDOUT.
if (pthread_cond_timedwait(&pSem->cond, &pSem->mtx, &tm)) {
pthread_mutex_unlock(&pSem->mtx); pthread_mutex_unlock(&pSem->mtx);
return FALSE; return FALSE;
} }

View File

@ -79,13 +79,12 @@ GHandle gwinGProgressbarCreate(GDisplay *g, GProgressbarObject *gs, const GWidge
gs->pos = 0; gs->pos = 0;
#if GWIN_PROGRESSBAR_AUTO #if GWIN_PROGRESSBAR_AUTO
gs->delay = 0;
gtimerInit(&gs->gt); gtimerInit(&gs->gt);
#endif #endif
ResetDisplayPos(gs); ResetDisplayPos(gs);
gwinSetVisible((GHandle)gs, pInit->g.show); gwinSetVisible((GHandle)gs, pInit->g.show);
return (GHandle)gs; return (GHandle)gs;
} }
@ -183,39 +182,37 @@ void gwinProgressbarDecrement(GHandle gh) {
static void _progressbarCallback(void *param) { static void _progressbarCallback(void *param) {
#define gsw ((GProgressbarObject *)gh) #define gsw ((GProgressbarObject *)gh)
GHandle gh = (GHandle)param; GHandle gh = (GHandle)param;
if (gh->vmt != (gwinVMT *)&progressbarVMT) if (gh->vmt != (gwinVMT *)&progressbarVMT)
return; return;
gwinProgressbarIncrement(gh); gwinProgressbarIncrement(gh);
if (gsw->pos < gsw->max) if (gsw->pos >= gsw->max)
gtimerStart(&(gsw->gt), _progressbarCallback, gh, FALSE, gsw->delay); gtimerStop(&gsw->gt);
#undef gsw
}
void gwinProgressbarStart(GHandle gh, delaytime_t delay) {
#define gsw ((GProgressbarObject *)gh)
if (gh->vmt != (gwinVMT *)&progressbarVMT)
return;
gsw->delay = delay;
gtimerStart(&(gsw->gt), _progressbarCallback, gh, FALSE, gsw->delay);
#undef gsw #undef gsw
} }
void gwinProgressbarStop(GHandle gh) { void gwinProgressbarStart(GHandle gh, delaytime_t delay) {
#define gsw ((GProgressbarObject *)gh) #define gsw ((GProgressbarObject *)gh)
if (gh->vmt != (gwinVMT *)&progressbarVMT) if (gh->vmt != (gwinVMT *)&progressbarVMT)
return; return;
gtimerStop(&(gsw->gt)); gtimerStart(&gsw->gt, _progressbarCallback, gh, TRUE, delay);
#undef gsw
}
void gwinProgressbarStop(GHandle gh) {
#define gsw ((GProgressbarObject *)gh)
if (gh->vmt != (gwinVMT *)&progressbarVMT)
return;
gtimerStop(&gsw->gt);
#undef gsw #undef gsw
} }
#endif /* GWIN_PROGRESSBAR_AUTO */ #endif /* GWIN_PROGRESSBAR_AUTO */
@ -226,7 +223,7 @@ void gwinProgressbarDecrement(GHandle gh) {
void gwinProgressbarDraw_Std(GWidgetObject *gw, void *param) { void gwinProgressbarDraw_Std(GWidgetObject *gw, void *param) {
#define gsw ((GProgressbarObject *)gw) #define gsw ((GProgressbarObject *)gw)
const GColorSet * pcol; const GColorSet * pcol;
(void) param; (void) param;

View File

@ -33,7 +33,6 @@ typedef struct GProgressbarObject {
int pos; int pos;
#if GWIN_PROGRESSBAR_AUTO #if GWIN_PROGRESSBAR_AUTO
GTimer gt; GTimer gt;
delaytime_t delay;
#endif #endif
} GProgressbarObject; } GProgressbarObject;
@ -59,7 +58,7 @@ extern "C" {
* @note A progressbar does not take any GINPUT inputs. * @note A progressbar does not take any GINPUT inputs.
* *
* @api * @api
*/ */
GHandle gwinGProgressbarCreate(GDisplay *g, GProgressbarObject *gb, const GWidgetInit *pInit); GHandle gwinGProgressbarCreate(GDisplay *g, GProgressbarObject *gb, const GWidgetInit *pInit);
#define gwinProgressbarCreate(w, pInit) gwinGProgressbarCreate(GDISP, w, pInit) #define gwinProgressbarCreate(w, pInit) gwinGProgressbarCreate(GDISP, w, pInit)
@ -165,14 +164,14 @@ void gwinProgressbarDecrement(GHandle gh);
* @api * @api
*/ */
void gwinProgressbarStart(GHandle gh, delaytime_t delay); void gwinProgressbarStart(GHandle gh, delaytime_t delay);
/** /**
* @brief Stop the timer which is started by @p gwinProgressbarStart() * @brief Stop the timer which is started by @p gwinProgressbarStart()
* *
* @param[in] gh The window handle (must be a progressbar window) * @param[in] gh The window handle (must be a progressbar window)
* *
* @api * @api
*/ */
void gwinProgressbarStop(GHandle gh); void gwinProgressbarStop(GHandle gh);
#endif /* GWIN_PROGRESSBAR_AUTO */ #endif /* GWIN_PROGRESSBAR_AUTO */