xref: /minix3/external/bsd/nvi/dist/common/pthread.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: pthread.c,v 1.3 2014/01/26 21:43:45 christos Exp $	*/
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 2000
484d9c625SLionel Sambuc  *	Sven Verdoolaege.  All rights reserved.
584d9c625SLionel Sambuc  *
684d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
784d9c625SLionel Sambuc  */
884d9c625SLionel Sambuc 
984d9c625SLionel Sambuc #include "config.h"
1084d9c625SLionel Sambuc 
11*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
12*0a6a1f1dSLionel Sambuc #if 0
1384d9c625SLionel Sambuc #ifndef lint
1484d9c625SLionel Sambuc 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 ";
1584d9c625SLionel Sambuc #endif /* not lint */
16*0a6a1f1dSLionel Sambuc #else
17*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: pthread.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
18*0a6a1f1dSLionel Sambuc #endif
1984d9c625SLionel Sambuc 
2084d9c625SLionel Sambuc #include <sys/types.h>
2184d9c625SLionel Sambuc #include <sys/queue.h>
2284d9c625SLionel Sambuc 
2384d9c625SLionel Sambuc #include <bitstring.h>
2484d9c625SLionel Sambuc #include <ctype.h>
2584d9c625SLionel Sambuc #include <errno.h>
2684d9c625SLionel Sambuc #include <stdio.h>
2784d9c625SLionel Sambuc #include <stdlib.h>
2884d9c625SLionel Sambuc #include <string.h>
2984d9c625SLionel Sambuc #include <unistd.h>
3084d9c625SLionel Sambuc 
3184d9c625SLionel Sambuc #include <pthread.h>
3284d9c625SLionel Sambuc 
3384d9c625SLionel Sambuc #include "../common/common.h"
3484d9c625SLionel Sambuc 
3584d9c625SLionel Sambuc static int vi_pthread_run __P((WIN *wp, void *(*fun)(void*), void *data));
3684d9c625SLionel Sambuc static int vi_pthread_lock_init __P((WIN *, void **));
3784d9c625SLionel Sambuc static int vi_pthread_lock_end __P((WIN *, void **));
3884d9c625SLionel Sambuc static int vi_pthread_lock_try __P((WIN *, void **));
3984d9c625SLionel Sambuc static int vi_pthread_lock_unlock __P((WIN *, void **));
4084d9c625SLionel Sambuc 
4184d9c625SLionel Sambuc /*
4284d9c625SLionel Sambuc  * thread_init
4384d9c625SLionel Sambuc  *
4484d9c625SLionel Sambuc  * PUBLIC: void thread_init __P((GS *gp));
4584d9c625SLionel Sambuc  */
4684d9c625SLionel Sambuc void
thread_init(GS * gp)4784d9c625SLionel Sambuc thread_init(GS *gp)
4884d9c625SLionel Sambuc {
4984d9c625SLionel Sambuc 	gp->run = vi_pthread_run;
5084d9c625SLionel Sambuc 	gp->lock_init = vi_pthread_lock_init;
5184d9c625SLionel Sambuc 	gp->lock_end = vi_pthread_lock_end;
5284d9c625SLionel Sambuc 	gp->lock_try = vi_pthread_lock_try;
5384d9c625SLionel Sambuc 	gp->lock_unlock = vi_pthread_lock_unlock;
5484d9c625SLionel Sambuc }
5584d9c625SLionel Sambuc 
5684d9c625SLionel Sambuc static int
vi_pthread_run(WIN * wp,void * (* fun)(void *),void * data)5784d9c625SLionel Sambuc vi_pthread_run(WIN *wp, void *(*fun)(void*), void *data)
5884d9c625SLionel Sambuc {
5984d9c625SLionel Sambuc 	pthread_t *t = malloc(sizeof(pthread_t));
6084d9c625SLionel Sambuc 	pthread_create(t, NULL, fun, data);
6184d9c625SLionel Sambuc 	return 0;
6284d9c625SLionel Sambuc }
6384d9c625SLionel Sambuc 
6484d9c625SLionel Sambuc static int
vi_pthread_lock_init(WIN * wp,void ** p)6584d9c625SLionel Sambuc vi_pthread_lock_init (WIN * wp, void **p)
6684d9c625SLionel Sambuc {
6784d9c625SLionel Sambuc 	pthread_mutex_t *mutex;
6884d9c625SLionel Sambuc 	int rc;
6984d9c625SLionel Sambuc 
7084d9c625SLionel Sambuc 	MALLOC_RET(NULL, mutex, pthread_mutex_t *, sizeof(*mutex));
7184d9c625SLionel Sambuc 
7284d9c625SLionel Sambuc 	if (rc = pthread_mutex_init(mutex, NULL)) {
7384d9c625SLionel Sambuc 		free(mutex);
7484d9c625SLionel Sambuc 		*p = NULL;
7584d9c625SLionel Sambuc 		return rc;
7684d9c625SLionel Sambuc 	}
7784d9c625SLionel Sambuc 	*p = mutex;
7884d9c625SLionel Sambuc 	return 0;
7984d9c625SLionel Sambuc }
8084d9c625SLionel Sambuc 
8184d9c625SLionel Sambuc static int
vi_pthread_lock_end(WIN * wp,void ** p)8284d9c625SLionel Sambuc vi_pthread_lock_end (WIN * wp, void **p)
8384d9c625SLionel Sambuc {
8484d9c625SLionel Sambuc 	int rc;
8584d9c625SLionel Sambuc 	pthread_mutex_t *mutex = (pthread_mutex_t *)*p;
8684d9c625SLionel Sambuc 
8784d9c625SLionel Sambuc 	if (rc = pthread_mutex_destroy(mutex))
8884d9c625SLionel Sambuc 		return rc;
8984d9c625SLionel Sambuc 
9084d9c625SLionel Sambuc 	free(mutex);
9184d9c625SLionel Sambuc 	*p = NULL;
9284d9c625SLionel Sambuc 	return 0;
9384d9c625SLionel Sambuc }
9484d9c625SLionel Sambuc 
9584d9c625SLionel Sambuc static int
vi_pthread_lock_try(WIN * wp,void ** p)9684d9c625SLionel Sambuc vi_pthread_lock_try (WIN * wp, void **p)
9784d9c625SLionel Sambuc {
9884d9c625SLionel Sambuc 	printf("try %p\n", *p);
9984d9c625SLionel Sambuc 	fflush(stdout);
10084d9c625SLionel Sambuc 	return 0;
10184d9c625SLionel Sambuc 	return pthread_mutex_trylock((pthread_mutex_t *)*p);
10284d9c625SLionel Sambuc }
10384d9c625SLionel Sambuc 
10484d9c625SLionel Sambuc static int
vi_pthread_lock_unlock(WIN * wp,void ** p)10584d9c625SLionel Sambuc vi_pthread_lock_unlock (WIN * wp, void **p)
10684d9c625SLionel Sambuc {
10784d9c625SLionel Sambuc 	printf("unlock %p\n", *p);
10884d9c625SLionel Sambuc 	return 0;
10984d9c625SLionel Sambuc 	return pthread_mutex_unlock((pthread_mutex_t *)*p);
11084d9c625SLionel Sambuc }
11184d9c625SLionel Sambuc 
112