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