This module is a set of virtual "system calls". This is an ANSI-C code, but the ANSI standard C library does not support some useful features of modern OSes (e.g. no multi-thread support is present). To make modules free of system dependent system calls (i.e. to make the project more portable) module "Virtual System API" is used in any other module when it must use non-ANSI features. The following structures are present:
To use shared data the following structures were prepared: semaphore_struct and ssp, defined as
typedef struct semaphore_struct *ssp;These structures are used to manage a "semaphore structure". The variable of the type ssp is returned by function creating this structure and used later to protect some shared data. There is no need to introduce the field of the structure. In Windows environment they are implemented using Mutexes and Events.
To manage multiple threads the following structures are used: thread_id and tidp, defined as:
typedef struct thread_id *tidp;
The following API has been prepared:
prepare_ss Allocates a new semaphore structure object.
Prepares a new semaphore structure, returns pointer to newly allocated structure or NULL if no data can be allocated.lockit Locks a semaphore.
Locks the semaphore that is pointed by ss_object. If someone has already locked the semaphore, this functions blocks. No processes using lock/unlock pair can come into critical section.unlockit Unlocks a semaphore.
Unlocks the semaphore that pointed by the ss_object semaphore object. If there is one blocked thread on this semaphore, it is unblocked and gets into the critical section. If multiple threads are blocked on the semaphore, one of them is unblocked.checklock Checks whether a semaphore is locked.
Returns 1 if semaphore is already locked or 0 if not.wait_for_resource Starts waiting for a "new resource".
Blocks waiting for an event (called here a "new resource"). This function must be called when inside critical section (lockit called before) because it unlocks this semaphore to allow the other thread go inside critical section to call new_resource (described below).new_resource Signals a "new resource".
If any other thread is waiting for a "new resource" (it called wait_for_resource function) this functions unblocks the waiting thread. Otherwise it does nothing.make_thread Creates a new thread.
Creates new thread with function function and parameters passed to it in param. Returns pointer to this thread or NULL if it cannot be created.kill_thread Kills (terminates) a running thread.
This function is called from the outside of the thread. This functions should be used with consciousness what it does in a given OS. It must not be used with threads having critical sections.exit_thread Terminates a thread.
This function is called from the inside of the thread, which wants to terminate itself (exit). retval is a return value.fallasleep Portable sleep().
Just falls asleep for seconds of seconds.put_message Prints a message.
Prints the message s. This function works like printf so multiple arguments are possible. In this, console version it calls printf.put_debug_message Prints a message when debugging.
Prints message only when _DEBUG is defined.