Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * OpenSPARC T2 Processor File: callback.c | |
5 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
7 | * | |
8 | * The above named program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public | |
10 | * License version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * The above named program is distributed in the hope that it will be | |
13 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public | |
18 | * License along with this work; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | * | |
21 | * ========== Copyright Header End ============================================ | |
22 | */ | |
23 | /* | |
24 | * Copyright 2006 Sun Microsystems, Inc. All rights reserved. | |
25 | * Use is subject to license terms. | |
26 | */ | |
27 | #pragma ident "@(#)callback.c 1.2 06/09/27 SMI" | |
28 | ||
29 | #include <stdlib.h> | |
30 | #include <pthread.h> | |
31 | ||
32 | #include "list.h" | |
33 | #include "allocate.h" | |
34 | #include "basics.h" | |
35 | #include "callback.h" | |
36 | ||
37 | typedef struct { | |
38 | callback_type_t type; | |
39 | cb_func_t *cb_func; | |
40 | void *cb_arg; | |
41 | } cb_entry_t; | |
42 | ||
43 | typedef LIST_DEF(cb_list_t, cb_entry_t); | |
44 | static cb_list_t cb_list; | |
45 | static pthread_mutex_t cb_lock; | |
46 | ||
47 | void | |
48 | callback_fire(callback_type_t type) | |
49 | { | |
50 | int i; | |
51 | ||
52 | /* can't grab cb_lock as funcs call callback_delete()... */ | |
53 | for (i = 0; i < cb_list.count; i++) { | |
54 | cb_entry_t *p; | |
55 | ||
56 | p = LIST_ENTRY(cb_list, i); | |
57 | if (p->type == type) | |
58 | (*p->cb_func)(p->cb_arg); | |
59 | } | |
60 | } | |
61 | ||
62 | void | |
63 | callback_register(callback_type_t type, cb_func_t func, void *arg) | |
64 | { | |
65 | cb_entry_t *p; | |
66 | int i; | |
67 | ||
68 | pthread_mutex_lock(&cb_lock); | |
69 | ||
70 | /* Check "free list". */ | |
71 | for (i = 0, p = NULL; i < cb_list.count; i++) { | |
72 | cb_entry_t *p1; | |
73 | p1 = LIST_ENTRY(cb_list, i); | |
74 | if (p1->type == CB_None) { | |
75 | p = p1; | |
76 | break; | |
77 | } | |
78 | } | |
79 | ||
80 | /* Allocate a new one? */ | |
81 | if (p == NULL) | |
82 | p = LIST_ADD(cb_list, cb_entry_t); | |
83 | ||
84 | p->type = type; | |
85 | p->cb_func = func; | |
86 | p->cb_arg = arg; | |
87 | pthread_mutex_unlock(&cb_lock); | |
88 | } | |
89 | ||
90 | void | |
91 | callback_delete(cb_func_t func) | |
92 | { | |
93 | int i; | |
94 | ||
95 | pthread_mutex_lock(&cb_lock); | |
96 | for (i = 0; i < cb_list.count; i++) { | |
97 | cb_entry_t *p; | |
98 | ||
99 | p = LIST_ENTRY(cb_list, i); | |
100 | if (p->cb_func == func) | |
101 | p->type = CB_None; /* Onto "free list". */ | |
102 | } | |
103 | pthread_mutex_unlock(&cb_lock); | |
104 | } | |
105 | ||
106 | void | |
107 | callback_init() | |
108 | { | |
109 | pthread_mutex_init(&cb_lock, NULL); | |
110 | } |