TABLE OF CONTENTS


/Semaphore [ Modules ]

[ Top ] [ Modules ]

DESCRIPTION

A Semaphore is an object dedicated to Task communication. It has a defined cound of tokens, a Task can aquire one as long as there is one left. If there are no more tokens left, the queue mode action takes place. Every Task can release a token.

SEE ALSO

    /Messagequeue, /Mutex, /Pipe, /Signal, /Syncpipe

Semaphore/Os_Semaphore_header [ Object Headers ]

[ Top ] [ Semaphore ] [ Object Headers ]

DESCRIPTION

Header structure of the Semaphore object

DECLARATION

Const Os_semaphore_hdr_taskqueue_ptr = 0
Const Os_semaphore_hdr_tokencount_ptr = Os_taskqueue_hdr_size + 0
Const Os_semaphore_hdr_tokensize_ptr = Os_taskqueue_hdr_size + 1
Const Os_semaphore_hdr_size = Os_taskqueue_hdr_size + 2

Semaphore/Os_semaphore_aquire [ Functions ]

[ Top ] [ Semaphore ] [ Functions ]

DESCRIPTION

Tries to aquire a Semaphore token. If there are no tokens left, the queue mode action takes place.

DECLARATION

Function Os_semaphore_aquire(byref Semaphore As Word , Byval Queuemode As Word) As Byte
   Local Tokencount As Byte
   'Local Tokensize As Byte   '

   Os_enter_critical
   Tokencount = Getbyte(Semaphore , Os_semaphore_hdr_tokencount_ptr)
   If Tokencount = 0 Then
      ' no tokens left, put in waiting list and suspend   '
      Os_sched_taskqueue_insert Semaphore , Os_task_active
      Os_exit_critical
      Select Case Queuemode
      Case Os_queuemode_noblock:
         ' return error   '
         Os_semaphore_aquire = False
         Exit Function
      Case Os_queuemode_block:
         ' suspend and wait to send a message   '
         Os_task_suspendmode Os_task_active , Os_task_suspend_nowakeup , 0
      Case Else
         ' suspend and wait to send a message or timeout   '
         Os_task_suspendmode Os_task_active , Os_task_suspend_timersingleshot , Queuemode
      End Select
      Os_task_suspend Os_task_active

      Os_enter_critical
      Tokencount = Getbyte(Semaphore , Os_semaphore_hdr_tokencount_ptr)
      If Tokencount = 0 Then
         Os_exit_critical
         Os_semaphore_aquire = False
         Exit Function
      End If
   End If

   'Tokensize = Getbyte(Semaphore , Os_semaphore_hdr_tokensize_ptr)   '
   Decr Tokencount
   Setbyte Semaphore , Os_semaphore_hdr_tokencount_ptr , Tokencount
   Os_exit_critical
   Os_semaphore_aquire = True
End Function

SEE ALSO

    Semaphore/Os_semaphore_release

Semaphore/Os_semaphore_create [ Functions ]

[ Top ] [ Semaphore ] [ Functions ]

DESCRIPTION

Creates a new Semaphore object.

DECLARATION

Function Os_semaphore_create(byval Tokens As Byte) As Word
   Local Semaphore As Word

   Semaphore = Malloc(os_semaphore_hdr_size)
   If Semaphore = 0 Then
      Os_semaphore_create = 0
      Exit Function
   End If
   Os_mem_clear Semaphore , Os_semaphore_hdr_size

   Setbyte Semaphore , Os_semaphore_hdr_tokensize_ptr , Tokens
   Setbyte Semaphore , Os_semaphore_hdr_tokencount_ptr , Tokens

   Os_semaphore_create = Semaphore
End Function

SEE ALSO

    Semaphore/Os_semaphore_kill

Semaphore/Os_semaphore_kill [ Functions ]

[ Top ] [ Semaphore ] [ Functions ]

DESCRIPTION

Kills a Semaphore object

DECLARATION

Sub Os_semaphore_kill(byref Semaphore As Word)
   ' TODO: Task waiting list not empty?   '
   Free Semaphore
End Sub

SEE ALSO

    Semaphore/Os_semaphore_create

Semaphore/Os_semaphore_release [ Functions ]

[ Top ] [ Semaphore ] [ Functions ]

DESCRIPTION

Releases a Semaphore token. The releasing Task must not have aquired it before.

DECLARATION

Sub Os_semaphore_release(byref Semaphore As Word)
   Local Tokencount As Byte
   Local Tokensize As Byte
   Local Task As Word

   Os_enter_critical
   Tokencount = Getbyte(Semaphore , Os_semaphore_hdr_tokencount_ptr)
   Tokensize = Getbyte(Semaphore , Os_semaphore_hdr_tokensize_ptr)

   If Tokencount < Tokensize Then
      ' release a token   '
      Incr Tokencount
      Setbyte Semaphore , Os_semaphore_hdr_tokencount_ptr , Tokencount
      Os_exit_critical

      ' let any waiting Task aquire the released token   '
      Task = Os_sched_taskqueue_remove(Semaphore)
      Os_exit_critical
      If Task <> 0 Then
         Os_event_task = Temp
         Os_task_event
      End If
   Else
      Os_exit_critical
   End If
End Sub

SEE ALSO

    Semaphore/Os_semaphore_aquire