comparison os2/dw.c @ 1987:21811c9e2eaf

OS/2: Fix memory and HAB/HMQ leak when compiled with Watcom C. OS/2 and Win: Fix resource leak when calling dw_thread_end().
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 15 Sep 2019 23:02:16 +0000
parents 0448507827e6
children c30f4354966e
comparison
equal deleted inserted replaced
1986:218fce0e3b06 1987:21811c9e2eaf
1 /* 1 /*
2 * Dynamic Windows: 2 * Dynamic Windows:
3 * A GTK like implementation of the PM GUI 3 * A GTK like implementation of the PM GUI
4 * 4 *
5 * (C) 2000-2017 Brian Smith <brian@dbsoft.org> 5 * (C) 2000-2019 Brian Smith <brian@dbsoft.org>
6 * (C) 2003-2011 Mark Hessling <mark@rexx.org> 6 * (C) 2003-2011 Mark Hessling <mark@rexx.org>
7 * (C) 2000 Achim Hasenmueller <achimha@innotek.de> 7 * (C) 2000 Achim Hasenmueller <achimha@innotek.de>
8 * (C) 2000 Peter Nielsen <peter@pmview.com> 8 * (C) 2000 Peter Nielsen <peter@pmview.com>
9 * (C) 2007 Alex Taylor (some code borrowed from clipuni) 9 * (C) 2007 Alex Taylor (some code borrowed from clipuni)
10 * (C) 1998 Sergey I. Yevtushenko (some code borrowed from cell toolkit) 10 * (C) 1998 Sergey I. Yevtushenko (some code borrowed from cell toolkit)
11925 * Generally an internal function called from a newly created 11925 * Generally an internal function called from a newly created
11926 * thread to setup the Dynamic Windows environment for the thread. 11926 * thread to setup the Dynamic Windows environment for the thread.
11927 * However it is exported so language bindings can call it when 11927 * However it is exported so language bindings can call it when
11928 * they create threads that require access to Dynamic Windows. 11928 * they create threads that require access to Dynamic Windows.
11929 */ 11929 */
11930 void API _dw_init_thread(void) 11930 void **_dw_init_thread2(void)
11931 { 11931 {
11932 HAB thishab = WinInitialize(0); 11932 HAB thishab = WinInitialize(0);
11933 HMQ thishmq = WinCreateMsgQueue(thishab, 0); 11933 HMQ thishmq = WinCreateMsgQueue(thishab, 0);
11934 #ifndef __WATCOMC__
11935 void **threadinfo = (void **)malloc(sizeof(void *) * 2); 11934 void **threadinfo = (void **)malloc(sizeof(void *) * 2);
11936 11935
11937 threadinfo[0] = (void *)thishab; 11936 threadinfo[0] = (void *)thishab;
11938 threadinfo[1] = (void *)thishmq; 11937 threadinfo[1] = (void *)thishmq;
11939 11938
11939 #ifndef __WATCOMC__
11940 *_threadstore() = (void *)threadinfo; 11940 *_threadstore() = (void *)threadinfo;
11941 #endif 11941 #endif
11942 11942
11943 #ifdef UNICODE 11943 #ifdef UNICODE
11944 /* Set the codepage to 1208 (UTF-8) */ 11944 /* Set the codepage to 1208 (UTF-8) */
11945 WinSetCp(thishmq, 1208); 11945 WinSetCp(thishmq, 1208);
11946 #endif 11946 #endif
11947 return threadinfo;
11948 }
11949
11950 void API _dw_init_thread(void)
11951 {
11952 _dw_init_thread2();
11947 } 11953 }
11948 11954
11949 /* 11955 /*
11950 * Generally an internal function called from a terminating 11956 * Generally an internal function called from a terminating
11951 * thread to cleanup the Dynamic Windows environment for the thread. 11957 * thread to cleanup the Dynamic Windows environment for the thread.
11952 * However it is exported so language bindings can call it when 11958 * However it is exported so language bindings can call it when
11953 * they exit threads that require access to Dynamic Windows. 11959 * they exit threads that require access to Dynamic Windows.
11954 */ 11960 */
11955 void API _dw_deinit_thread(void) 11961 void _dw_deinit_thread2(void **threadinfo)
11956 { 11962 {
11957 #ifndef __WATCOMC__ 11963 #ifndef __WATCOMC__
11958 void **threadinfo = (void **)*_threadstore(); 11964 if(!threadinfo)
11959 11965 threadinfo = (void **)*_threadstore();
11966 #endif
11967
11960 if(threadinfo) 11968 if(threadinfo)
11961 { 11969 {
11962 HAB thishab = (HAB)threadinfo[0]; 11970 HAB thishab = (HAB)threadinfo[0];
11963 HMQ thishmq = (HMQ)threadinfo[1]; 11971 HMQ thishmq = (HMQ)threadinfo[1];
11964 11972
11965 WinDestroyMsgQueue(thishmq); 11973 WinDestroyMsgQueue(thishmq);
11966 WinTerminate(thishab); 11974 WinTerminate(thishab);
11967 free(threadinfo); 11975 free(threadinfo);
11968 } 11976 }
11969 #endif 11977 }
11978
11979 void API _dw_deinit_thread(void)
11980 {
11981 _dw_deinit_thread2(NULL);
11970 } 11982 }
11971 11983
11972 /* 11984 /*
11973 * Encapsulate the message queues on OS/2. 11985 * Encapsulate the message queues on OS/2.
11974 */ 11986 */
11975 void _dwthreadstart(void *data) 11987 void _dwthreadstart(void *data)
11976 { 11988 {
11977 void (API_FUNC threadfunc)(void *) = NULL; 11989 void (API_FUNC threadfunc)(void *) = NULL;
11978 void **tmp = (void **)data; 11990 void **tmp = (void **)data;
11979 11991 void **threadinfo = _dw_init_thread2();
11980 _dw_init_thread();
11981 11992
11982 threadfunc = (void (API_FUNC)(void *))tmp[0]; 11993 threadfunc = (void (API_FUNC)(void *))tmp[0];
11983 threadfunc(tmp[1]); 11994 threadfunc(tmp[1]);
11984 11995
11985 free(tmp); 11996 free(tmp);
11986 11997
11987 _dw_deinit_thread(); 11998 _dw_deinit_thread2(threadinfo);
11988 } 11999 }
11989 12000
11990 /* 12001 /*
11991 * Creates a new thread with a starting point of func. 12002 * Creates a new thread with a starting point of func.
11992 * Parameters: 12003 * Parameters:
12014 /* 12025 /*
12015 * Ends execution of current thread immediately. 12026 * Ends execution of current thread immediately.
12016 */ 12027 */
12017 void API dw_thread_end(void) 12028 void API dw_thread_end(void)
12018 { 12029 {
12030 _dw_deinit_thread();
12019 _endthread(); 12031 _endthread();
12020 } 12032 }
12021 12033
12022 /* 12034 /*
12023 * Returns the current thread's ID. 12035 * Returns the current thread's ID.