2
/*___INFO__MARK_BEGIN__*/
3
/*************************************************************************
5
* The Contents of this file are made available subject to the terms of
6
* the Sun Industry Standards Source License Version 1.2
8
* Sun Microsystems Inc., March, 2001
11
* Sun Industry Standards Source License Version 1.2
12
* =================================================
13
* The contents of this file are subject to the Sun Industry Standards
14
* Source License Version 1.2 (the "License"); You may not use this file
15
* except in compliance with the License. You may obtain a copy of the
16
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
18
* Software provided under this License is provided on an "AS IS" basis,
19
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
20
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
21
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
22
* See the License for the specific provisions governing your rights and
23
* obligations concerning the Software.
25
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
27
* Copyright: 2001 by Sun Microsystems, Inc.
29
* All Rights Reserved.
31
************************************************************************/
32
/*___INFO__MARK_END__*/
42
cl_raw_list_t* thread_list = NULL;
44
void *my_thread(void *t_conf);
45
void *my_thread_test(void *t_conf);
49
#ifdef __CL_FUNCTION__
50
#undef __CL_FUNCTION__
52
#define __CL_FUNCTION__ "main()"
55
cl_thread_settings_t* thread_p = NULL;
56
cl_thread_settings_t* dummy_thread_p = NULL;
60
/* setup thread list */
61
cl_thread_list_setup(&thread_list,"thread list");
63
/* setup first thread */
64
cl_thread_list_create_thread(thread_list, &dummy_thread_p,NULL, "1st thread", 1, my_thread, NULL, NULL);
66
/* setup second thread */
67
cl_thread_list_create_thread(thread_list, &dummy_thread_p, NULL, "2nd thread", 2, my_thread, NULL, NULL);
69
thread_p = cl_thread_list_get_thread_by_id(thread_list, 1);
70
printf("first thread (%s) has id: %d\n", thread_p->thread_name, thread_p->thread_id);
72
thread_p = cl_thread_list_get_thread_by_name(thread_list, "2nd thread");
73
printf("second thread (%s) has id: %d\n" , thread_p->thread_name, thread_p->thread_id);
75
while (count++ < 10) {
78
printf("we have %ld threads.\n", cl_raw_list_get_elem_count(thread_list));
80
thread_p = cl_thread_list_get_first_thread(thread_list);
81
id = thread_p->thread_id;
83
printf("-----------> delete thread %d ...\n", id);
84
cl_thread_list_delete_thread_by_id(thread_list, id);
85
printf("<----------- delete thread %d done\n", id);
87
printf("-----------> add thread ...\n");
88
cl_thread_list_create_thread(thread_list, &dummy_thread_p, NULL,"new thread", id, my_thread, NULL, NULL);
89
printf("<----------- add thread done\n");
93
/* delete all threads */
94
while ( (thread_p=cl_thread_list_get_first_thread(thread_list)) != NULL ) {
95
int id = thread_p->thread_id;
96
printf("-----------> delete thread %d ...\n",id );
98
cl_thread_list_delete_thread(thread_list, thread_p);
99
printf("<----------- delete thread %d done\n",id );
103
cl_thread_list_cleanup(&thread_list);
104
printf("main done\n");
110
#ifdef __CL_FUNCTION__
111
#undef __CL_FUNCTION__
113
#define __CL_FUNCTION__ "my_thread()"
114
void *my_thread(void *t_conf) {
115
cl_thread_settings_t* thread_p = NULL;
118
/* get pointer to cl_thread_settings_t struct */
119
cl_thread_settings_t *thread_config = (cl_thread_settings_t*)t_conf;
122
printf("thread %d: initialize\n", thread_config->thread_id);
126
/* thread init done, trigger startup conditon variable*/
127
cl_thread_func_startup(thread_config);
130
printf("thread %d: enter mainloop\n", thread_config->thread_id);
132
/* ok, thread main */
133
while (do_exit == 0) {
136
printf("thread %d: try locking thread_list ...\n", thread_config->thread_id);
138
/* check for cancel */
139
cl_thread_func_testcancel(thread_config);
140
if (cl_raw_list_lock(thread_list) == CL_RETVAL_OK) {
142
printf("thread %d: locked thread_list\n", thread_config->thread_id);
144
if (thread_config->thread_id == 1) {
145
thread_p = cl_thread_list_get_thread_by_id(thread_list,2);
148
thread_p = cl_thread_list_get_thread_by_id(thread_list,1);
152
if (thread_config->thread_id == 1) {
153
cl_thread_trigger_event(thread_p);
155
printf("thread %d: triggered %d events\n", thread_config->thread_id, counter);
158
printf("thread %d: thread %d not found\n", thread_config->thread_id,id);
160
printf("thread %d: unlocking thread_list\n", thread_config->thread_id);
162
cl_raw_list_unlock(thread_list);
165
if ((ret_val = cl_thread_wait_for_event(thread_config,0,10000)) != CL_RETVAL_OK) { /* nothing to do */
167
case CL_RETVAL_CONDITION_WAIT_TIMEOUT:
168
/* printf("thread %d: got timeout\n", thread_config->thread_id); */
171
printf("thread %d: got error: %d\n", thread_config->thread_id, ret_val);
178
printf("thread %d: exit\n", thread_config->thread_id);
180
/* at least set exit state */
181
cl_thread_func_cleanup(thread_config);