xref: /haiku/src/libs/compat/freebsd_network/mutex.c (revision f099314535596f3d1119302d6921087c9bcd2946)
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_DEF) {
25 		mutex_init_etc(&mutex->u.mutex.lock, name, MUTEX_FLAG_CLONE_NAME);
26 		mutex->u.mutex.owner = -1;
27 	} else if (options == MTX_RECURSE) {
28 		recursive_lock_init_etc(&mutex->u.recursive, name,
29 			MUTEX_FLAG_CLONE_NAME);
30 	} else
31 		panic("fbsd: unsupported mutex type");
32 
33 	mutex->type = options;
34 }
35 
36 
37 void
38 mtx_destroy(struct mtx *mutex)
39 {
40 	if (mutex->type == MTX_DEF)
41 		mutex_destroy(&mutex->u.mutex.lock);
42 	else if (mutex->type == MTX_RECURSE)
43 		recursive_lock_destroy(&mutex->u.recursive);
44 }
45 
46 
47 status_t
48 init_mutexes()
49 {
50 	mtx_init(&Giant, "Banana Giant", NULL, MTX_DEF);
51 	rw_lock_init(&ifnet_rwlock, "gDevices");
52 	mtx_init(&gIdStoreLock, "Identity Store", NULL, MTX_DEF);
53 
54 	return B_OK;
55 }
56 
57 
58 void
59 uninit_mutexes()
60 {
61 	mtx_destroy(&Giant);
62 	rw_lock_destroy(&ifnet_rwlock);
63 	mtx_destroy(&gIdStoreLock);
64 }
65