home *** CD-ROM | disk | FTP | other *** search
- /*
- --- Version 2.0 89-12-14 12:50 ---
-
- TSKCNT.C - CTask - Counter handling routines.
-
- Public Domain Software written by
- Thomas Wagner
- Patschkauer Weg 31
- D-1000 Berlin 33
- West Germany
- */
-
- #include <stdio.h>
-
- #include "tsk.h"
- #include "tsklocal.h"
-
- /*
- create_counter - initialises counter.
- */
-
- counterptr far create_counter (counterptr cnt
- #if (TSK_NAMEPAR)
- ,byteptr name
- #endif
- )
- {
- #if (TSK_DYNAMIC)
- if (cnt == NULL)
- {
- if ((cnt = tsk_alloc (sizeof (counter))) == NULL)
- return NULL;
- cnt->flags = F_TEMP;
- }
- else
- cnt->flags = 0;
- #endif
-
- tsk_init_qhead (&cnt->wait_set);
- tsk_init_qhead (&cnt->wait_clear);
- cnt->state = 0;
-
- #if (TSK_NAMED)
- tsk_add_name (&cnt->name, name, TYP_COUNTER, cnt);
- #endif
-
- return cnt;
- }
-
-
- /*
- delete_counter - kills all processes waiting for counter
- */
-
- void far delete_counter (counterptr cnt)
- {
- CRITICAL;
-
- C_ENTER;
- tsk_kill_queue (&(cnt->wait_set));
- tsk_kill_queue (&(cnt->wait_clear));
- cnt->state = 0L;
- C_LEAVE;
-
- #if (TSK_NAMED)
- tsk_del_name (&cnt->name);
- #endif
-
- #if (TSK_DYNAMIC)
- if (cnt->flags & F_TEMP)
- tsk_free (cnt);
- #endif
- }
-
-
- /*
- clear_counter - Sets counter to zero. All tasks waiting for
- Counter zero are made eligible.
- */
-
- void far clear_counter (counterptr cnt)
- {
- CRITICAL;
-
- C_ENTER;
- cnt->state = 0L;
- tsk_runable_all (&cnt->wait_clear);
- C_LEAVE;
- }
-
-
- /*
- wait_counter_set - Wait until counter is != 0. If counter is != 0 on
- entry, the counter is decremented and the task
- continues to run. If the counter is decremented to
- zero, tasks waiting for zero are made eligible.
- */
-
- int far wait_counter_set (counterptr cnt, dword timeout)
- {
- CRITICAL;
-
- C_ENTER;
- if (cnt->state)
- {
- if (!--cnt->state)
- tsk_runable_all (&cnt->wait_clear);
- C_LEAVE;
- return 0;
- }
-
- GLOBDATA current_task->retptr = NULL;
- tsk_wait (&cnt->wait_set, timeout);
- return (int)GLOBDATA current_task->retptr;
- }
-
- /*
- wait_counter_clear - Wait until counter is == 0. If counter is == 0 on
- entry, the task continues to run.
- */
-
- int far wait_counter_clear (counterptr cnt, dword timeout)
- {
- CRITICAL;
-
- C_ENTER;
- if (!cnt->state)
- {
- C_LEAVE;
- return 0;
- }
-
- GLOBDATA current_task->retptr = NULL;
- tsk_wait (&cnt->wait_clear, timeout);
- return (int)GLOBDATA current_task->retptr;
- }
-
-
- /*
- inc_counter - Increment counter. If there are tasks waiting for the
- set state, the first task in the queue is made eligible.
- */
-
- void far inc_counter (counterptr cnt)
- {
- CRITICAL;
-
- C_ENTER;
- if (!cnt->wait_set.first->kind)
- {
- cnt->state++;
- C_LEAVE;
- return;
- }
- tsk_runable ((tcbptr)cnt->wait_set.first);
- C_LEAVE;
- }
-
-
- /*
- check_counter - return current counter state.
- */
-
- dword far check_counter (counterptr cnt)
- {
- return cnt->state;
- }
-
-
-
- /*
- set_counter - Sets counter to given value. Depending on the value,
- tasks waiting for counter zero or counter set are made
- eligible.
- */
-
- void far set_counter (counterptr cnt, dword val)
- {
- CRITICAL;
-
- C_ENTER;
- while (val && cnt->wait_set.first->kind)
- {
- tsk_runable ((tcbptr)cnt->wait_set.first);
- val--;
- }
- if (!val)
- tsk_runable_all (&cnt->wait_clear);
- cnt->state = val;
- C_LEAVE;
- }
-
-