xref: /minix3/external/bsd/nvi/dist/common/pthread.c (revision b5e2faaaaf60a8b9a02f8d72f64caa56a87eb312)
1 /*	$NetBSD: pthread.c,v 1.2 2013/11/22 15:52:05 christos Exp $	*/
2 /*-
3  * Copyright (c) 2000
4  *	Sven Verdoolaege.  All rights reserved.
5  *
6  * See the LICENSE file for redistribution information.
7  */
8 
9 #include "config.h"
10 
11 #ifndef lint
12 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 ";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 #include <sys/queue.h>
17 
18 #include <bitstring.h>
19 #include <ctype.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include <pthread.h>
27 
28 #include "../common/common.h"
29 
30 static int vi_pthread_run __P((WIN *wp, void *(*fun)(void*), void *data));
31 static int vi_pthread_lock_init __P((WIN *, void **));
32 static int vi_pthread_lock_end __P((WIN *, void **));
33 static int vi_pthread_lock_try __P((WIN *, void **));
34 static int vi_pthread_lock_unlock __P((WIN *, void **));
35 
36 /*
37  * thread_init
38  *
39  * PUBLIC: void thread_init __P((GS *gp));
40  */
41 void
42 thread_init(GS *gp)
43 {
44 	gp->run = vi_pthread_run;
45 	gp->lock_init = vi_pthread_lock_init;
46 	gp->lock_end = vi_pthread_lock_end;
47 	gp->lock_try = vi_pthread_lock_try;
48 	gp->lock_unlock = vi_pthread_lock_unlock;
49 }
50 
51 static int
52 vi_pthread_run(WIN *wp, void *(*fun)(void*), void *data)
53 {
54 	pthread_t *t = malloc(sizeof(pthread_t));
55 	pthread_create(t, NULL, fun, data);
56 	return 0;
57 }
58 
59 static int
60 vi_pthread_lock_init (WIN * wp, void **p)
61 {
62 	pthread_mutex_t *mutex;
63 	int rc;
64 
65 	MALLOC_RET(NULL, mutex, pthread_mutex_t *, sizeof(*mutex));
66 
67 	if (rc = pthread_mutex_init(mutex, NULL)) {
68 		free(mutex);
69 		*p = NULL;
70 		return rc;
71 	}
72 	*p = mutex;
73 	return 0;
74 }
75 
76 static int
77 vi_pthread_lock_end (WIN * wp, void **p)
78 {
79 	int rc;
80 	pthread_mutex_t *mutex = (pthread_mutex_t *)*p;
81 
82 	if (rc = pthread_mutex_destroy(mutex))
83 		return rc;
84 
85 	free(mutex);
86 	*p = NULL;
87 	return 0;
88 }
89 
90 static int
91 vi_pthread_lock_try (WIN * wp, void **p)
92 {
93 	printf("try %p\n", *p);
94 	fflush(stdout);
95 	return 0;
96 	return pthread_mutex_trylock((pthread_mutex_t *)*p);
97 }
98 
99 static int
100 vi_pthread_lock_unlock (WIN * wp, void **p)
101 {
102 	printf("unlock %p\n", *p);
103 	return 0;
104 	return pthread_mutex_unlock((pthread_mutex_t *)*p);
105 }
106 
107