if we're intentionally using power-of-two sizing for chunks, don't add the header...
[mono-project/dkf.git] / mono / io-layer / collection.c
blobfff2187d2edda240429cb1ab377f3d7469af99f7
1 /*
2 * collection.c: Garbage collection for handles
4 * Author:
5 * Dick Porter (dick@ximian.com)
7 * (C) 2004-2006 Novell, Inc.
8 */
10 #include <config.h>
11 #include <glib.h>
12 #include <pthread.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <unistd.h>
17 #include <mono/io-layer/wapi.h>
18 #include <mono/io-layer/collection.h>
19 #include <mono/io-layer/handles-private.h>
21 #define LOGDEBUG(...)
22 // #define LOGDEBUG(...) g_message(__VA_ARGS__)
24 static pthread_t collection_thread_id;
26 static gpointer collection_thread (gpointer unused G_GNUC_UNUSED)
28 struct timespec sleepytime;
30 sleepytime.tv_sec = _WAPI_HANDLE_COLLECTION_UPDATE_INTERVAL;
31 sleepytime.tv_nsec = 0;
33 while (_wapi_has_shut_down == FALSE) {
34 nanosleep (&sleepytime, NULL);
36 //_wapi_handle_dump ();
37 _wapi_handle_update_refs ();
40 pthread_exit (NULL);
42 return(NULL);
45 void _wapi_collection_init (void)
47 pthread_attr_t attr;
48 int ret;
49 int set_stacksize = 0;
51 retry:
52 ret = pthread_attr_init (&attr);
53 g_assert (ret == 0);
55 #if defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
56 if (set_stacksize == 0) {
57 ret = pthread_attr_setstacksize (&attr, MAX (65536, PTHREAD_STACK_MIN));
58 g_assert (ret == 0);
59 } else if (set_stacksize == 1) {
60 ret = pthread_attr_setstacksize (&attr, 131072);
61 g_assert (ret == 0);
63 #endif
65 ret = pthread_create (&collection_thread_id, &attr, collection_thread,
66 NULL);
67 if (ret != 0 && set_stacksize < 2) {
68 set_stacksize++;
69 goto retry;
71 if (ret != 0) {
72 g_error ("%s: Couldn't create handle collection thread: %s",
73 __func__, g_strerror (ret));
77 void _wapi_handle_collect (void)
79 guint32 count = _wapi_shared_layout->collection_count;
80 int i, thr_ret;
82 if (!_wapi_shm_enabled ())
83 return;
85 LOGDEBUG ("%s: (%d) Starting a collection", __func__, _wapi_getpid ());
87 /* Become the collection master */
88 thr_ret = _wapi_handle_lock_shared_handles ();
89 g_assert (thr_ret == 0);
91 LOGDEBUG ("%s: (%d) Master set", __func__, _wapi_getpid ());
93 /* If count has changed, someone else jumped in as master */
94 if (count == _wapi_shared_layout->collection_count) {
95 guint32 too_old = (guint32)(time(NULL) & 0xFFFFFFFF) - _WAPI_HANDLE_COLLECTION_EXPIRED_INTERVAL;
97 for (i = 0; i < _WAPI_HANDLE_INITIAL_COUNT; i++) {
98 struct _WapiHandleShared *data;
100 data = &_wapi_shared_layout->handles[i];
101 if (data->timestamp < too_old) {
102 LOGDEBUG ("%s: (%d) Deleting handle 0x%x", __func__, _wapi_getpid (), i);
103 memset (&_wapi_shared_layout->handles[i], '\0', sizeof(struct _WapiHandleShared));
107 for (i = 0; i < _wapi_fileshare_layout->hwm; i++) {
108 struct _WapiFileShare *file_share = &_wapi_fileshare_layout->share_info[i];
110 if (file_share->timestamp < too_old) {
111 memset (file_share, '\0',
112 sizeof(struct _WapiFileShare));
116 InterlockedIncrement ((gint32 *)&_wapi_shared_layout->collection_count);
119 _wapi_handle_unlock_shared_handles ();
121 LOGDEBUG ("%s: (%d) Collection done", __func__, _wapi_getpid ());