xref: /haiku/src/libs/compat/freebsd_network/mutex.c (revision dba28784c21beab5d397068303881fe024a76859)
1 /*
2  * Copyright 2009, Colin Günther, coling@gmx.de.
3  * Copyright 2007, Hugo Santos. All Rights Reserved.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "device.h"
9 
10 #include <compat/sys/mutex.h>
11 
12 
13 // these methods are bit unfriendly, a bit too much panic() around
14 
15 struct mtx Giant;
16 struct rw_lock ifnet_rwlock;
17 struct mtx gIdStoreLock;
18 
19 
20 void
21 mtx_init(struct mtx *mutex, const char *name, const char *type,
22 	int options)
23 {
24 	if ((options & MTX_RECURSE) != 0) {
25 		recursive_lock_init_etc(&mutex->u.recursive, name,
26 			MUTEX_FLAG_CLONE_NAME);
27 	} else {
28 		mutex_init_etc(&mutex->u.mutex.lock, name, MUTEX_FLAG_CLONE_NAME);
29 		mutex->u.mutex.owner = -1;
30 	}
31 
32 	mutex->type = options;
33 }
34 
35 
36 void
37 mtx_sysinit(void *arg)
38 {
39 	struct mtx_args *margs = arg;
40 
41 	mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
42 	    margs->ma_opts);
43 }
44 
45 
46 void
47 mtx_destroy(struct mtx *mutex)
48 {
49 	if ((mutex->type & MTX_RECURSE) != 0)
50 		recursive_lock_destroy(&mutex->u.recursive);
51 	else
52 		mutex_destroy(&mutex->u.mutex.lock);
53 }
54 
55 
56 status_t
57 init_mutexes()
58 {
59 	mtx_init(&Giant, "Banana Giant", NULL, MTX_DEF);
60 	rw_lock_init(&ifnet_rwlock, "gDevices");
61 	mtx_init(&gIdStoreLock, "Identity Store", NULL, MTX_DEF);
62 
63 	return B_OK;
64 }
65 
66 
67 void
68 uninit_mutexes()
69 {
70 	mtx_destroy(&Giant);
71 	rw_lock_destroy(&ifnet_rwlock);
72 	mtx_destroy(&gIdStoreLock);
73 }
74