changeset 89:90e3c9c7ac73

Added Mutex and unnamed Event semaphore functions and classes. Also some various cleanups to a few other functions.
author Brian Smith <brian@dbsoft.org>
date Thu, 26 Sep 2013 01:50:58 -0500
parents c1370f669734
children b90ce8ec9c44
files src/dw/dw.go src/dw/dwglue.c
diffstat 2 files changed, 186 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/dw/dw.go	Tue Sep 10 03:54:16 2013 -0500
+++ b/src/dw/dw.go	Thu Sep 26 01:50:58 2013 -0500
@@ -108,6 +108,12 @@
     hcont HANDLE
     filesystem bool
 }
+type HEV struct {
+    hev unsafe.Pointer
+}
+type HMTX struct {
+    hmtx unsafe.Pointer
+}
 type HICN unsafe.Pointer
 type HTIMER struct {
     tid C.int
@@ -807,6 +813,22 @@
    return 0;
 }
 
+func (event HEV) GetHandle() unsafe.Pointer {
+   return event.hev;
+}
+
+func (window HEV) GetType() C.uint {
+   return 0;
+}
+
+func (mutex HMTX) GetHandle() unsafe.Pointer {
+   return mutex.hmtx;
+}
+
+func (window HMTX) GetType() C.uint {
+   return 0;
+}
+
 // Initializes the Dynamic Windows engine.
 func Init(newthread int) int {
     if len(os.Args) > 0 {
@@ -4289,14 +4311,17 @@
 
 // Starts a new query of a container.
 func Container_query_start(handle HANDLE, flags uint) string {
-   cresult := C.go_container_query_start(handle.GetHandle(), C.ulong(flags));
+   cresult := C.go_container_query_start(handle.GetHandle(), C.ulong(flags) &^ C.DW_CR_RETDATA);
    result := C.GoString(cresult);
-   if((flags & C.DW_CR_RETDATA) == 0) {
-      C.dw_free(unsafe.Pointer(cresult));
-   }
+   defer C.dw_free(unsafe.Pointer(cresult));
    return result;
 }
 
+func Container_query_start_data(handle HANDLE, flags uint) POINTER {
+   cresult := C.go_container_query_start(handle.GetHandle(), C.ulong(flags) | C.DW_CR_RETDATA);
+   return POINTER(cresult);
+}
+
 // Starts a new query of a container.
 func (handle HCONTAINER) QueryStart(flags uint) string {
     return Container_query_start(handle, flags);
@@ -4304,14 +4329,17 @@
 
 // Continues an existing query of a container.
 func Container_query_next(handle HANDLE, flags uint) string {
-   cresult := C.go_container_query_next(handle.GetHandle(), C.ulong(flags));
+   cresult := C.go_container_query_next(handle.GetHandle(), C.ulong(flags) &^ C.DW_CR_RETDATA);
    result := C.GoString(cresult);
-   if((flags & C.DW_CR_RETDATA) == 0) {
-      C.dw_free(unsafe.Pointer(cresult));
-   }
+   defer C.dw_free(unsafe.Pointer(cresult));
    return result;
 }
 
+func Container_query_next_data(handle HANDLE, flags uint) POINTER {
+   cresult := C.go_container_query_next(handle.GetHandle(), C.ulong(flags) | C.DW_CR_RETDATA);
+   return POINTER(cresult);
+}
+
 // Continues an existing query of a container.
 func (handle HCONTAINER) QueryNext(flags uint) string {
     return Container_query_next(handle, flags);
@@ -4649,6 +4677,106 @@
    runtime.LockOSThread();
 }
 
+// Creates a new mutex semaphore.
+func Mutex_new() HMTX {
+   return HMTX{C.go_mutex_new()};
+}
+
+// Creates a new mutex semaphore.
+func MutexNew() HMTX {
+    return Mutex_new();
+}
+
+// Closes a semaphore created by Mutex_new().
+func Mutex_close(handle HMTX) {
+    C.go_mutex_close(unsafe.Pointer(handle.hmtx));
+}
+
+// Closes a semaphore created by MutexNew().
+func (handle HMTX) Close() {
+    Mutex_close(handle);
+}
+
+// Tries to gain access to the semaphore, if it can't it blocks.
+func Mutex_lock(handle HMTX) {
+    C.go_mutex_lock(unsafe.Pointer(handle.hmtx));
+}
+
+// Tries to gain access to the semaphore, if it can't it blocks.
+func (handle HMTX) Lock() {
+    Mutex_lock(handle);
+}
+
+// Reliquishes the access to the semaphore.
+func Mutex_unlock(handle HMTX) {
+    C.go_mutex_unlock(unsafe.Pointer(handle.hmtx));
+}
+
+// Reliquishes the access to the semaphore.
+func (handle HMTX) Unlock() {
+    Mutex_lock(handle);
+}
+
+// Tries to gain access to the semaphore.
+func Mutex_trylock(handle HMTX) int {
+    return int(C.go_mutex_trylock(unsafe.Pointer(handle.hmtx)));
+}
+
+// Tries to gain access to the semaphore.
+func (handle HMTX) TryLock() int {
+    return Mutex_trylock(handle);
+}
+
+// Creates an unnamed event semaphore.
+func Event_new() HEV {
+   return HEV{C.go_event_new()};
+}
+
+// Creates an unnamed event semaphore.
+func EventNew() HEV {
+    return Event_new();
+}
+
+// Closes a semaphore created by Event_new().
+func Event_close(handle HEV) int {
+    return int(C.go_event_close(unsafe.Pointer(handle.hev)));
+}
+
+// Closes a semaphore created by EventNew().
+func (handle HEV) Close() int {
+    return Event_close(handle);
+}
+
+// Posts a semaphore created by Event_new(). Causing all threads waiting on this event in Event_wait() to continue.
+func Event_post(handle HEV) int {
+    return int(C.go_event_post(unsafe.Pointer(handle.hev)));
+}
+
+// Posts a semaphore created by EventNew(). Causing all threads waiting on this event in Wait() to continue.
+func (handle HEV) Post() int {
+    return Event_post(handle);
+}
+
+// Resets a semaphore created by Event_new().
+func Event_reset(handle HEV) int {
+    return int(C.go_event_reset(unsafe.Pointer(handle.hev)));
+}
+
+// Resets a semaphore created by EventNew().
+func (handle HEV) Reset() int {
+    return Event_reset(handle);
+}
+
+// Waits on a semaphore created by Event_new(), until the event gets posted or until the timeout expires.
+func Event_wait(handle HEV, timeout int) int {
+    return int(C.go_event_wait(unsafe.Pointer(handle.hev), C.ulong(timeout)));
+}
+
+// Waits on a semaphore created by EventNew(), until the event gets posted or until the timeout expires.
+func (handle HEV) Wait(timeout int) int {
+    return Event_wait(handle, timeout);
+}
+
 var go_flags_no_data C.uint = 1;
 
 // Connect a function or closure to a window close (delete) event.
--- a/src/dw/dwglue.c	Tue Sep 10 03:54:16 2013 -0500
+++ b/src/dw/dwglue.c	Thu Sep 26 01:50:58 2013 -0500
@@ -1101,6 +1101,56 @@
     return dw_print_cancel((HPRINT)print);
 }
 
+static void *go_mutex_new(void)
+{
+    return (void *)dw_mutex_new();
+}
+
+static void go_mutex_close(void *mutex)
+{
+    dw_mutex_close((HMTX)mutex);
+}
+
+static void go_mutex_lock(void *mutex)
+{
+    dw_mutex_lock((HMTX)mutex);
+}
+
+static void go_mutex_unlock(void *mutex)
+{
+    dw_mutex_unlock((HMTX)mutex);
+}
+
+static int go_mutex_trylock(void *mutex)
+{
+    return dw_mutex_trylock((HMTX)mutex);
+}
+
+static void *go_event_new(void)
+{
+    return (void *)dw_event_new();
+}
+
+static int go_event_close(void *event)
+{
+    return dw_event_close((HMTX)event);
+}
+
+static int go_event_post(void *event)
+{
+    return dw_event_post((HMTX)event);
+}
+
+static int go_event_reset(void *event)
+{
+    return dw_event_reset((HMTX)event);
+}
+
+static int go_event_wait(void *event, unsigned long timeout)
+{
+    return dw_event_wait((HMTX)event, timeout);
+}
+
 extern int go_int_callback_basic(void *pfunc, void* window, void *data, unsigned int flags);
 extern int go_int_callback_configure(void *pfunc, void* window, int width, int height, void *data, unsigned int flags);
 extern int go_int_callback_keypress(void *pfunc, void *window, char ch, int vk, int state, void *data, char *utf8, unsigned int flags);