Process, Thread, CPU, and Mutex Functions
CosmProcessIDSyntax#include "cosm/os_task.h" u64 CosmProcessID( void ); DescriptionGets the ID of the current process. Return ValuesThe ID of the current process, 0 indicates failure. ErrorsPossible causes of failure:
Exampleu64 pid; pid = CosmProcessID(); CosmProcessPrioritySyntax#include "cosm/os_task.h" u8 CosmProcessPriority( u8 priority ); DescriptionGet or try and set the current process priority. If priority is 0 no change is made. Otherwise the priority is set to priority. Priorities are mapped within the range 1-255. You cannot increase the priority above the default normal priority (255) for the OS. Not all OSs will let you return to a higher priority after you reduce it. Return ValuesProcess priority (mapped to 1-255 scale) after call is completed, 0 indicates failure. ErrorsPossible causes of failure:
Exampleu8 priority; u8 new_priority; priority = CosmProcessPriority( (u8) 0 ); if ( priority < 100 ) { priority =+ 40; } new_priority = CosmProcessPriority( priority ); if ( new_priority == 0 ) { /* Error */ } CosmProcessSpawnSyntax#include "cosm/os_task.h" s32 CosmProcessSpawn( u64 * process_id, const ascii * command, const ascii * arguments, ... ); DescriptionSpawn a new process running command. The command must be a full path in Cosm format to the binary. process_id is set to the process ID of the swawned process. The arguments MUST end with a NULL. Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsPossible causes of an failure:
Exampleu64 child; if ( CosmProcessSpawn( &child, "cat", "file.txt", "file2.txt", NULL ) != COSM_PASS ) { CosmPrint( "Spawn Failed\n" ); } else { CosmPrint( "Child PID = %v\n", child ); } CosmProcessEndSyntax#include "cosm/os_task.h" void CosmProcessEnd( int status ); DescriptionEnds the current process, and all threads. Uses status as the returning exit code. Programs must always exit this way, or system resources may not be freed on certain operating systems. Return ValuesNone. ErrorsNone. ExampleCosmProcessEnd( 1 ); CosmCPUCountSyntax#include "cosm/os_task.h" s32 CosmCPUCount( u32 * count ); DescriptionSets count to the number of CPUs in the system. Note that count will be set to 1 on failure since machines with zero CPUs do not exist. Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsPossible causes of failure:
Exampleu32 num_cpus; if ( CosmCPUCount( &num_cpus ) == COSM_FAIL ) { /* error, but num_cpus is 1 */ } CosmCPULockSyntax#include "cosm/os_task.h" s32 CosmCPULock( u64 process_id, u32 cpu ); DescriptionLocks the process process_id, and any threads, to the CPU cpu. This function can be used for a large variety of performance and security reasons. Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsPossible causes of failure:
Exampleu64 pid; u32 cpunum; s32 result; pid = CosmProcessID(); if ( pid == 0 ) { /* Error */ } result = CosmCPUGet( &cpunum ); if ( result != COSM_PASS ) { /* Error */ } result = CosmCPULock( pid, cpunum ); if ( result != COSM_PASS ) { /* Error */ } CosmCPUUnlockSyntax#include "cosm/os_task.h" s32 CosmCPUUnlock( u64 process_id ); DescriptionUnlocks the process process_id and its threads from a CPU. If you are using any secure memory, calling this function would be a bad idea. Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsPossible causes of failure:
Exampleu64 pid; u32 cpunum; s32 result; pid = CosmProcessID(); if ( pid == 0 ) { /* Error */ } result = CosmCPUGet( &cpunum ); if ( result != COSM_PASS ) { /* Error */ } result = CosmCPULock( pid, cpunum ); if ( result != COSM_PASS ) { /* Error */ } result = CosmCPUUnlock( pid ); if ( result != COSM_PASS ) { /* Error */ } CosmThreadBeginSyntax#include "cosm/os_task.h" s32 CosmThreadBegin( u64 * thread_id, void (*start)(void *), void * arg, u32 stack_size ); DescriptionBegin a thread using the function start with argument arg with a stack size of stack_size. thread_id is set to the thread ID of the new thread. Stack size not supported with all OSes. Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsPossible causes of failure:
Examplevoid thread_function( void * arg ) { u32 * puppy; puppy = (u32 *) arg; /* thread code */ } /* ... */ u64 thread_id; u32 data; data = 42; if ( CosmThreadBegin( &thread_id, thread_function, (void *) &data, 4096 ) != COSM_PASS ) { /* thread failed to start */ } CosmThreadIDSyntax#include "cosm/os_task.h" u64 CosmThreadID( void ); DescriptionGets the current thread ID. Return ValuesThe current thread ID, 0 indicates failure. ErrorsPossible causes of failure:
Exampleu64 thread_id; thread_id = CosmThreadID(); CosmThreadPrioritySyntax#include "cosm/os_task.h" u8 CosmThreadPriority( u8 priority ); DescriptionGet or try and set the current thread priority. If priority is 0 no change is made. Otherwise the priority is set to priority. Priorities are mapped within the range 1-255. You cannot increase the priority above the default normal priority (255) for the OS. Return ValuesThread priority (mapped to 1-255 scale) after call is completed, 0 indicates failure. ErrorsPossible causes of failure:
Exampleu8 priority; u8 new_priority; priority = CosmThreadPriority( (u8) 0 ); if ( priority < 100 ) { priority =+ 40; } new_priority = CosmThreadPriority( priority ); if ( new_priority == 0 ) { /* Error */ } CosmThreadEndSyntax#include "cosm/os_task.h" void CosmThreadEnd( void ); DescriptionEnds the calling thread. Return ValuesNone. ErrorsNone. ExampleCosmThreadEnd(); CosmMutexLockSyntax#include "cosm/os_task.h" s32 CosmMutexLock( cosm_MUTEX * mutex, u32 wait ); DescriptionSet an exclusive lock. You should never have more then one lock alive at a time per thread, as this may result in deadlock. Note that if you create a mutex you MUST free it eventually with CosmMutexFree. Wait Modes:
Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsPossible causes of failure:
Examplecosm_MUTEX lock; CosmMemSet( &lock, sizeof( cosm_MUTEX ), 0 ); if ( CosmMutexLock( &lock, COSM_MUTEX_WAIT ) != COSM_PASS ) { /* bad error */ } /* do critical work */ CosmMutexUnlock( &lock ); while ( CosmMutexLock( &lock, COSM_MUTEX_NOWAIT ) != COSM_PASS ) { /* mutex was already locked, do something */ } /* do critical work */ CosmMutexUnlock( &lock ); CosmMutexFree( &lock ); CosmMutexUnlockSyntax#include "cosm/os_task.h" s32 CosmMutexUnlock( cosm_MUTEX * mutex ); DescriptionUnset an exclusive lock. Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsNone. Examplecosm_MUTEX lock; CosmMemSet( &lock, sizeof( cosm_MUTEX ), 0 ); if ( CosmMutexLock( &lock, COSM_MUTEX_WAIT ) != COSM_PASS ) { /* bad error */ } /* do critical work */ CosmMutexUnlock( &lock ); CosmMutexFree( &lock ); CosmMutexFreeSyntax#include "cosm/os_task.h" void CosmMutexFree( cosm_MUTEX * mutex ); DescriptionFree the mutex and related system resources. Do not call this unless you are completely done with the mutex, very bad things will happen. Return ValuesNone. ErrorsNone. Examplecosm_MUTEX lock; CosmMemSet( &lock, sizeof( cosm_MUTEX ), 0 ); if ( CosmMutexLock( &lock, COSM_MUTEX_WAIT ) != COSM_PASS ) { /* bad error */ } /* do critical work */ CosmMutexUnlock( &lock ); CosmMutexFree( &lock ); CosmSleepSyntax#include "cosm/os_task.h" void CosmSleep( u32 millisec ); DescriptionSleep for millisec milliseconds. Return ValuesNone. ErrorsNone. Example/* sleep for 10 seconds */ CosmSleep( 10000 ); CosmYieldSyntax#include "cosm/os_task.h" void CosmYield( void ); DescriptionYield to another thread/process that wants to run. Return ValuesNone. ErrorsNone. ExampleCosmYield(); CosmSignalSyntax#include "cosm/os_task.h" s32 CosmSignal( u64 process_id, u32 signal ); DescriptionSends signal to process process_id. Signals:
Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsPossible causes of failure:
Exampleu64 pid; s32 result; pid = CosmProcessID(); result = CosmSignal( pid, COSM_SIGNAL_PING ); if ( result != COSM_PASS ) { /* Target process didn't exist */ } CosmSignalRegisterSyntax#include "cosm/os_task.h" s32 CosmSignalRegister( u32 signal_type, void (*handler)(int) ); DescriptionRegister handler as the handler for signal_type. signal_type must be either COSM_SIGNAL_INT or COSM_SIGNAL_TERM. See CosmSignal for sending signals. Once the handler is triggered the OS will reset the handler, so if you wish to reuse a signal, you must reregister it in the handler. Inside of your signal handler you must be very careful as any thread may end up running the handler at any time. Make your handler as short as possible. Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsPossible causes of failure:
Examplevoid signal_function( int arg ) { /* signal code */ CosmSignalRegister( COSM_SIGNAL_INT, signal_function ); /* set a flag or other handler actions */ } /* ... */ if ( CosmSignalRegister( COSM_SIGNAL_INT, signal_function ) != COSM_PASS ) { /* error */ } CosmSystemClockSyntax#include "cosm/os_task.h" s32 CosmSystemClock( cosmtime * clock ); DescriptionSets the value of clock from the system clock. cosmtime is a signed number (s128) of seconds in 64b.64b fixed point format based on the time 0 = 00:00:00 UTC, Jan 1, 2000 AD. Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsPossible causes of failure:
Examplecosmtime clock; if ( CosmSystemClock( &clock ) != COSM_PASS ) { /* temporal rift */ }
© Copyright Mithral Communications & Design Inc.
1995-2024.
All rights reserved.
Mithral® and Cosm® are trademarks of
Mithral Communications & Design Inc.
|