summaryrefslogtreecommitdiffstats
path: root/otherlibs/systhreads
diff options
context:
space:
mode:
authorXavier Leroy <xavier.leroy@inria.fr>2000-02-10 14:04:59 +0000
committerXavier Leroy <xavier.leroy@inria.fr>2000-02-10 14:04:59 +0000
commit9e206909f48d5d2579b6ec17764d3273df23ff08 (patch)
tree3319a3e0c3383ed812f781859aadffd7f1462fdf /otherlibs/systhreads
parent7175ab048dcaaa39649ebc386ae37750baaf27e1 (diff)
Introduction des blocs de type Custom.
Remplacement des blocs de type Final par des blocs de type Custom. git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@2804 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
Diffstat (limited to 'otherlibs/systhreads')
-rw-r--r--otherlibs/systhreads/posix.c40
-rw-r--r--otherlibs/systhreads/win32.c45
2 files changed, 72 insertions, 13 deletions
diff --git a/otherlibs/systhreads/posix.c b/otherlibs/systhreads/posix.c
index 5f3c56d0f..bc58b4bc2 100644
--- a/otherlibs/systhreads/posix.c
+++ b/otherlibs/systhreads/posix.c
@@ -24,6 +24,7 @@
#include <sys/time.h>
#include "alloc.h"
#include "callback.h"
+#include "custom.h"
#include "fail.h"
#include "io.h"
#include "memory.h"
@@ -444,7 +445,7 @@ value caml_thread_join(value th) /* ML */
/* Mutex operations */
-#define Mutex_val(v) ((pthread_mutex_t *) Field(v, 1))
+#define Mutex_val(v) (* ((pthread_mutex_t **) Data_custom_val(v)))
#define Max_mutex_number 1000
static void caml_mutex_finalize(value wrapper)
@@ -454,14 +455,31 @@ static void caml_mutex_finalize(value wrapper)
stat_free(mut);
}
+static int caml_mutex_condition_compare(value wrapper1, value wrapper2)
+{
+ pthread_mutex_t * mut1 = Mutex_val(wrapper1);
+ pthread_mutex_t * mut2 = Mutex_val(wrapper2);
+ return mut1 == mut2 ? 0 : mut1 < mut2 ? -1 : 1;
+}
+
+static struct custom_operations caml_mutex_ops = {
+ "_mutex",
+ caml_mutex_finalize,
+ caml_mutex_condition_compare,
+ custom_hash_default,
+ custom_serialize_default,
+ custom_deserialize_default
+};
+
value caml_mutex_new(value unit) /* ML */
{
pthread_mutex_t * mut;
value wrapper;
mut = stat_alloc(sizeof(pthread_mutex_t));
caml_pthread_check(pthread_mutex_init(mut, NULL), "Mutex.create");
- wrapper = alloc_final(2, caml_mutex_finalize, 1, Max_mutex_number);
- Field(wrapper, 1) = (value) mut;
+ wrapper = alloc_custom(&caml_mutex_ops, sizeof(pthread_mutex_t *),
+ 1, Max_mutex_number);
+ Mutex_val(wrapper) = mut;
return wrapper;
}
@@ -503,7 +521,7 @@ value caml_mutex_try_lock(value wrapper) /* ML */
/* Conditions operations */
-#define Condition_val(v) ((pthread_cond_t *) Field(v, 1))
+#define Condition_val(v) (* ((pthread_cond_t **) Data_custom_val(v)))
#define Max_condition_number 1000
static void caml_condition_finalize(value wrapper)
@@ -513,14 +531,24 @@ static void caml_condition_finalize(value wrapper)
stat_free(cond);
}
+static struct custom_operations caml_condition_ops = {
+ "_condition",
+ caml_condition_finalize,
+ caml_mutex_condition_compare,
+ custom_hash_default,
+ custom_serialize_default,
+ custom_deserialize_default
+};
+
value caml_condition_new(value unit) /* ML */
{
pthread_cond_t * cond;
value wrapper;
cond = stat_alloc(sizeof(pthread_cond_t));
caml_pthread_check(pthread_cond_init(cond, NULL), "Condition.create");
- wrapper = alloc_final(2, caml_condition_finalize, 1, Max_condition_number);
- Field(wrapper, 1) = (value) cond;
+ wrapper = alloc_custom(&caml_condition_ops, sizeof(pthread_cond_t *),
+ 1, Max_condition_number);
+ Condition_val(wrapper) = cond;
return wrapper;
}
diff --git a/otherlibs/systhreads/win32.c b/otherlibs/systhreads/win32.c
index 35a26ae39..463bd3b98 100644
--- a/otherlibs/systhreads/win32.c
+++ b/otherlibs/systhreads/win32.c
@@ -18,6 +18,7 @@
#include <signal.h>
#include "alloc.h"
#include "callback.h"
+#include "custom.h"
#include "fail.h"
#include "io.h"
#include "memory.h"
@@ -432,7 +433,7 @@ value caml_thread_join(value th) /* ML */
/* Mutex operations */
-#define Mutex_val(v) (*((HANDLE *)(&Field(v, 1))))
+#define Mutex_val(v) (*((HANDLE *) Data_custom_val(v)))
#define Max_mutex_number 1000
static void caml_mutex_finalize(value mut)
@@ -440,11 +441,26 @@ static void caml_mutex_finalize(value mut)
CloseHandle(Mutex_val(mut));
}
+static int caml_mutex_compare(value wrapper1, value wrapper2)
+{
+ HANDLE h1 = Mutex_val(wrapper1);
+ HANDLE h2 = Mutex_val(wrapper2);
+ return mut1 == mut2 ? 0 : mut1 < mut2 ? -1 : 1;
+}
+
+static struct custom_operations caml_mutex_ops = {
+ "_mutex",
+ caml_mutex_finalize,
+ caml_mutex_condition_compare,
+ custom_hash_default,
+ custom_serialize_default,
+ custom_deserialize_default
+};
+
value caml_mutex_new(value unit) /* ML */
{
value mut;
- mut = alloc_final(1 + sizeof(HANDLE) / sizeof(value),
- caml_mutex_finalize, 1, Max_mutex_number);
+ mut = alloc_custom(&caml_mutex_ops, sizeof(HANDLE), 1, Max_mutex_number);
Mutex_val(mut) = CreateMutex(0, FALSE, NULL);
if (Mutex_val(mut) == NULL) caml_wthread_error("Mutex.create");
return mut;
@@ -496,12 +512,11 @@ value caml_thread_delay(value val) /* ML */
/* Conditions operations */
struct caml_condvar {
- void (*final_fun)(); /* Finalization function */
unsigned long count; /* Number of waiting threads */
HANDLE sem; /* Semaphore on which threads are waiting */
};
-#define Condition_val(v) ((struct caml_condvar *)(v))
+#define Condition_val(v) ((struct caml_condvar *) Data_custom_val(v))
#define Max_condition_number 1000
static void caml_condition_finalize(value cond)
@@ -509,11 +524,27 @@ static void caml_condition_finalize(value cond)
CloseHandle(Condition_val(cond)->sem);
}
+static int caml_condition_compare(value wrapper1, value wrapper2)
+{
+ HANDLE h1 = Condition_val(wrapper1)->sem;
+ HANDLE h2 = Condition_val(wrapper2)->sem;
+ return mut1 == mut2 ? 0 : mut1 < mut2 ? -1 : 1;
+}
+
+static struct custom_operations caml_condition_ops = {
+ "_condition",
+ caml_condition_finalize,
+ caml_condition_compare,
+ custom_hash_default,
+ custom_serialize_default,
+ custom_deserialize_default
+};
+
value caml_condition_new(value unit) /* ML */
{
value cond;
- cond = alloc_final(sizeof(struct caml_condvar) / sizeof(value),
- caml_condition_finalize, 1, Max_condition_number);
+ cond = alloc_custom(&caml_condition_ops, sizeof(struct caml_condvar),
+ 1, Max_condition_number);
Condition_val(cond)->sem = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL);
if (Condition_val(cond)->sem == NULL)
caml_wthread_error("Condition.create");