[io-layer] Header usage grooming. Remove some unused headers and add config.h guards...
[mono.git] / mono / io-layer / collection.c
1 /*
2  * collection.c:  Garbage collection for handles
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *
7  * (C) 2004-2006 Novell, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12 #include <pthread.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15
16 #include <mono/io-layer/wapi.h>
17 #include <mono/io-layer/collection.h>
18 #include <mono/io-layer/handles-private.h>
19 #include <mono/utils/atomic.h>
20
21 #if 0
22 // #define DEBUG(...) g_message(__VA_ARGS__)
23 #else
24 #define DEBUG(...)
25 #endif
26
27 static pthread_t collection_thread_id;
28
29 static gpointer collection_thread (gpointer unused G_GNUC_UNUSED)
30 {
31         struct timespec sleepytime;
32
33         sleepytime.tv_sec = _WAPI_HANDLE_COLLECTION_UPDATE_INTERVAL;
34         sleepytime.tv_nsec = 0;
35
36         while (_wapi_has_shut_down == FALSE) {
37                 nanosleep (&sleepytime, NULL);
38
39                 //_wapi_handle_dump ();
40                 _wapi_handle_update_refs ();
41         }
42
43         pthread_exit (NULL);
44
45         return(NULL);
46 }
47
48 void _wapi_collection_init (void)
49 {
50         pthread_attr_t attr;
51         int ret;
52         int set_stacksize = 0;
53
54  retry:
55         ret = pthread_attr_init (&attr);
56         g_assert (ret == 0);
57
58 #if defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
59         if (set_stacksize == 0) {
60                         ret = pthread_attr_setstacksize (&attr, MAX (65536, PTHREAD_STACK_MIN));
61                         g_assert (ret == 0);
62         } else if (set_stacksize == 1) {
63                         ret = pthread_attr_setstacksize (&attr, 131072);
64                         g_assert (ret == 0);
65         }
66 #endif
67
68         ret = pthread_create (&collection_thread_id, &attr, collection_thread,
69                               NULL);
70         if (ret != 0 && set_stacksize < 2) {
71                 set_stacksize++;
72                 goto retry;
73         }
74         if (ret != 0) {
75                 g_error ("%s: Couldn't create handle collection thread: %s",
76                          __func__, g_strerror (ret));
77         }
78 }
79
80 void _wapi_handle_collect (void)
81 {
82         guint32 count = _wapi_shared_layout->collection_count;
83         int i, thr_ret;
84
85         if (!_wapi_shm_enabled ())
86                 return;
87         
88         DEBUG ("%s: (%d) Starting a collection", __func__, _wapi_getpid ());
89
90         /* Become the collection master */
91         thr_ret = _wapi_handle_lock_shared_handles ();
92         g_assert (thr_ret == 0);
93         
94         thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
95         g_assert (thr_ret == 0);
96         
97         DEBUG ("%s: (%d) Master set", __func__, _wapi_getpid ());
98         
99         /* If count has changed, someone else jumped in as master */
100         if (count == _wapi_shared_layout->collection_count) {
101                 guint32 too_old = (guint32)(time(NULL) & 0xFFFFFFFF) - _WAPI_HANDLE_COLLECTION_EXPIRED_INTERVAL;
102
103                 for (i = 0; i < _WAPI_HANDLE_INITIAL_COUNT; i++) {
104                         struct _WapiHandleShared *data;
105                         
106                         data = &_wapi_shared_layout->handles[i];
107                         if (data->timestamp < too_old) {
108                                 DEBUG ("%s: (%d) Deleting handle 0x%x", __func__, _wapi_getpid (), i);
109                                 memset (&_wapi_shared_layout->handles[i], '\0', sizeof(struct _WapiHandleShared));
110                         }
111                 }
112
113                 for (i = 0; i < _wapi_fileshare_layout->hwm; i++) {
114                         struct _WapiFileShare *file_share = &_wapi_fileshare_layout->share_info[i];
115                         
116                         if (file_share->timestamp < too_old) {
117                                 memset (file_share, '\0',
118                                         sizeof(struct _WapiFileShare));
119                         }
120                 }
121
122                 InterlockedIncrement ((gint32 *)&_wapi_shared_layout->collection_count);
123         }
124         
125         thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
126         g_assert (thr_ret == 0);
127         
128         _wapi_handle_unlock_shared_handles ();
129
130         DEBUG ("%s: (%d) Collection done", __func__, _wapi_getpid ());
131 }