home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CTASK.ZIP
/
TSKRSC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-12-20
|
4KB
|
196 lines
/*
--- Version 2.0 89-12-14 13:16 ---
TSKRSC.C - CTask - Resource 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 resource - initialise resource.
*/
resourceptr far create_resource (resourceptr rsc
#if (TSK_NAMEPAR)
,byteptr name
#endif
)
{
#if (TSK_DYNAMIC)
if (rsc == NULL)
{
if ((rsc = tsk_alloc (sizeof (resource))) == NULL)
return NULL;
rsc->flags = F_TEMP;
}
else
rsc->flags = 0;
#endif
tsk_init_qhead (&rsc->waiting);
rsc->count = 0;
rsc->owner = NULL;
#if (TSK_NAMED)
tsk_add_name (&rsc->name, name, TYP_RESOURCE, rsc);
#endif
return rsc;
}
/*
delete_resource - Kill all tasks waiting for the resource.
*/
void far delete_resource (resourceptr rsc)
{
CRITICAL;
C_ENTER;
tsk_kill_queue (&(rsc->waiting));
rsc->owner = NULL;
rsc->count = 1;
C_LEAVE;
#if (TSK_NAMED)
tsk_del_name (&rsc->name);
#endif
#if (TSK_DYNAMIC)
if (rsc->flags & F_TEMP)
tsk_free (rsc);
#endif
}
/*
request_resource - Wait until resource is available. If the resource
is free on entry, it is assigned to the task, and
the task continues to run.
If the task requesting the resource already owns it,
this routine returns 0.
*/
int far request_resource (resourceptr rsc, dword timeout)
{
CRITICAL;
C_ENTER;
if (!rsc->count || rsc->owner == GLOBDATA current_task)
{
rsc->count = 1;
rsc->owner = GLOBDATA current_task;
C_LEAVE;
return 0;
}
GLOBDATA current_task->retptr = NULL;
tsk_wait (&rsc->waiting, timeout);
return (int)GLOBDATA current_task->retptr;
}
/*
c_request_resource - If the resource is free on entry, it is assigned
to the task, otherwise an error status is returned.
*/
int far c_request_resource (resourceptr rsc)
{
int rv;
CRITICAL;
C_ENTER;
if ((rv = (!rsc->count || rsc->owner == GLOBDATA current_task)) != 0)
{
rsc->count = 1;
rsc->owner = GLOBDATA current_task;
}
C_LEAVE;
return (rv) ? 0 : -1;
}
int far request_cresource (resourceptr rsc, dword timeout)
{
CRITICAL;
C_ENTER;
if (!rsc->count || rsc->owner == GLOBDATA current_task)
{
rsc->count++;
rsc->owner = GLOBDATA current_task;
C_LEAVE;
return 0;
}
GLOBDATA current_task->retptr = NULL;
tsk_wait (&rsc->waiting, timeout);
return (int)GLOBDATA current_task->retptr;
}
int far c_request_cresource (resourceptr rsc)
{
int rv;
CRITICAL;
C_ENTER;
if ((rv = (!rsc->count || rsc->owner == GLOBDATA current_task)) != 0)
{
rsc->count++;
rsc->owner = GLOBDATA current_task;
}
C_LEAVE;
return (rv) ? 0 : -1;
}
/*
release_resource - Release the resource. If there are tasks waiting for
the resource, it is assigned to the first waiting
task, and the task is made eligible.
*/
void far release_resource (resourceptr rsc)
{
tcbptr curr;
CRITICAL;
if (rsc->owner != GLOBDATA current_task)
return;
C_ENTER;
if (!--rsc->count)
{
if (!(curr = (tcbptr)rsc->waiting.first)->cqueue.kind)
{
rsc->owner = NULL;
C_LEAVE;
return;
}
rsc->count++;
rsc->owner = curr;
tsk_runable (curr);
}
C_LEAVE;
}
/*
check_resource - returns 1 if the resource is available.
*/
int far check_resource (resourceptr rsc)
{
return rsc->count == 0;
}