rename O_DIRECTORY to O_TMP_DIRECTORY to avoid conflict with open option
[nvi.git] / common / pthread.c
blobc6e3013ae0742ff5fcf4006be146eb014a7adcf4
1 /*-
2 * Copyright (c) 2000
3 * Sven Verdoolaege. All rights reserved.
5 * See the LICENSE file for redistribution information.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "$Id: pthread.c,v 1.4 2000/07/22 14:52:37 skimo Exp $ (Berkeley) $Date: 2000/07/22 14:52:37 $";
12 #endif /* not lint */
14 #include <sys/types.h>
15 #include <sys/queue.h>
17 #include <bitstring.h>
18 #include <ctype.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include <pthread.h>
27 #include "../common/common.h"
29 static int vi_pthread_run __P((WIN *wp, void *(*fun)(void*), void *data));
30 static int vi_pthread_lock_init __P((WIN *, void **));
31 static int vi_pthread_lock_end __P((WIN *, void **));
32 static int vi_pthread_lock_try __P((WIN *, void **));
33 static int vi_pthread_lock_unlock __P((WIN *, void **));
36 * thread_init
38 * PUBLIC: void thread_init __P((GS *gp));
40 void
41 thread_init(GS *gp)
43 gp->run = vi_pthread_run;
44 gp->lock_init = vi_pthread_lock_init;
45 gp->lock_end = vi_pthread_lock_end;
46 gp->lock_try = vi_pthread_lock_try;
47 gp->lock_unlock = vi_pthread_lock_unlock;
50 static int
51 vi_pthread_run(WIN *wp, void *(*fun)(void*), void *data)
53 pthread_t *t = malloc(sizeof(pthread_t));
54 pthread_create(t, NULL, fun, data);
55 return 0;
58 static int
59 vi_pthread_lock_init (WIN * wp, void **p)
61 pthread_mutex_t *mutex;
62 int rc;
64 MALLOC_RET(NULL, mutex, pthread_mutex_t *, sizeof(*mutex));
66 if (rc = pthread_mutex_init(mutex, NULL)) {
67 free(mutex);
68 *p = NULL;
69 return rc;
71 *p = mutex;
72 return 0;
75 static int
76 vi_pthread_lock_end (WIN * wp, void **p)
78 int rc;
79 pthread_mutex_t *mutex = (pthread_mutex_t *)*p;
81 if (rc = pthread_mutex_destroy(mutex))
82 return rc;
84 free(mutex);
85 *p = NULL;
86 return 0;
89 static int
90 vi_pthread_lock_try (WIN * wp, void **p)
92 printf("try %p\n", *p);
93 fflush(stdout);
94 return 0;
95 return pthread_mutex_trylock((pthread_mutex_t *)*p);
98 static int
99 vi_pthread_lock_unlock (WIN * wp, void **p)
101 printf("unlock %p\n", *p);
102 return 0;
103 return pthread_mutex_unlock((pthread_mutex_t *)*p);