SemaSet.st
author Claus Gittinger <cg@exept.de>
Fri, 15 Jan 1999 21:53:59 +0100
changeset 3956 51f1a9a4d63f
parent 2876 fb3fed7470be
permissions -rw-r--r--
changes for egcs (stdio uses __new / utsname)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     1
"
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     2
 COPYRIGHT (c) 1995 by Stefan Vogel / Claus Gittinger
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     3
              All Rights Reserved
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     4
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     5
 This software is furnished under a license and may be used
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     6
 only in accordance with the terms of that license and with the
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     8
 be provided or otherwise made available to, or used by, any
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
     9
 other person.  No title to or ownership of the software is
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    10
 hereby transferred.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    11
"
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    12
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    13
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    14
IdentitySet subclass:#SemaphoreSet
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    15
	instanceVariableNames:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    16
	classVariableNames:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    17
	poolDictionaries:''
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    18
	category:'Kernel-Processes'
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    19
!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    20
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    21
!SemaphoreSet class methodsFor:'documentation'!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    22
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    23
copyright
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    24
"
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    25
 COPYRIGHT (c) 1995 by Stefan Vogel / Claus Gittinger
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    26
              All Rights Reserved
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    27
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    28
 This software is furnished under a license and may be used
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    29
 only in accordance with the terms of that license and with the
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    31
 be provided or otherwise made available to, or used by, any
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    32
 other person.  No title to or ownership of the software is
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    33
 hereby transferred.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    34
"
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    35
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    36
!
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    37
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    38
documentation
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    39
"
2876
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
    40
    SemaphoreSets allow waiting until one of several semaphores becomes available.
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
    41
    They provide a waiting protocol which is compatible to Semaphore, 
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
    42
    i.e. #wait and #waitWithTimeOut.
1273
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
    43
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
    44
    [see also:]
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
    45
        Semaphore
f8449f53a6a3 commentary
Claus Gittinger <cg@exept.de>
parents: 1031
diff changeset
    46
        Process ProcessorScheduler
1294
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    47
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    48
    [author:]
e26bbb61f6b2 documentation
Claus Gittinger <cg@exept.de>
parents: 1273
diff changeset
    49
        Stefan Vogel
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    50
"
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    51
!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    52
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    53
examples
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
    54
"
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    55
 the following example forks a process which waits on any
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    56
 of sema1, sema2 to be signalled. The main thread signals those.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    57
 ---------------------------------------------------------------
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    58
                                                                        [exBegin]
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    59
    |sema1 sema2 semaSet proc|
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    60
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    61
    sema1 := Semaphore new.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    62
    sema2 := Semaphore new.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    63
    semaSet := SemaphoreSet with:sema1 with:sema2.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    64
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    65
    proc := [
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    66
        [
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    67
            |ret name|
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    68
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    69
            ret := semaSet wait.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    70
            ret == sema1 ifTrue:[
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    71
                name := 'sema1'
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    72
            ] ifFalse:[ 
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    73
                ret == sema2 ifTrue:[
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    74
                    name := 'sema2'
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    75
                ]
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    76
            ].
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
    77
            Transcript showCR: name, ' raised'.
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    78
            ret == sema2 ifTrue:[
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    79
                proc terminate
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    80
            ]
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    81
        ] loop
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    82
    ] fork.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    83
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    84
    (Delay forSeconds:3) wait.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    85
    sema1 signal.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    86
    (Delay forSeconds:3) wait.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    87
    sema2 signal.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    88
                                                                        [exEnd]
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    89
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    90
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    91
 the following example forks a process which waits on any
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    92
 of sema1, sema2 to be signalled, or a timeout to occur.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
    93
 ---------------------------------------------------------------
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
    94
                                                                        [exBegin]
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
    95
    |sema1 sema2 semaSet proc|
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
    96
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
    97
    sema1 := Semaphore new.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
    98
    sema2 := Semaphore new.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
    99
    semaSet := SemaphoreSet with:sema1 with:sema2.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   100
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   101
    proc := [
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   102
        [
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   103
            |ret name|
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   104
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   105
            ret := semaSet waitWithTimeout:5.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   106
            ret == sema1 ifTrue:[
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   107
                name := 'sema1'
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   108
            ] ifFalse:[ 
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   109
                ret == sema2 ifTrue:[
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   110
                    name := 'sema2'
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   111
                ] ifFalse:[
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   112
                    name := ret printString
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   113
                ]
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   114
            ].
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
   115
            Transcript showCR: name, ' raised'.
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   116
            ret isNil ifTrue:[
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   117
                proc terminate
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   118
            ]
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   119
        ] loop
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   120
    ] fork.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   121
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   122
    (Delay forSeconds:3) wait.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   123
    sema1 signal.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   124
    (Delay forSeconds:3) wait.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   125
    sema2 signal.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   126
                                                                        [exEnd]
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   127
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   128
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   129
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   130
 the following example forks a process which waits on input
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   131
 to arrive on any of 2 sharedQueues (with timeout)
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   132
 The main thread writes data into those queues.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   133
 ---------------------------------------------------------------
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   134
                                                                        [exBegin]
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   135
    |q1 q2 semaSet proc|
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   136
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   137
    q1 := SharedQueue new.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   138
    q2 := SharedQueue new.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   139
    semaSet := SemaphoreSet with:(q1 readSemaphore) with:(q2 readSemaphore).
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   140
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   141
    proc := [
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   142
        [
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   143
            |ret whatHappened|
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   144
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   145
            ret := semaSet waitWithTimeout:5.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   146
            ret == q1 readSemaphore ifTrue:[
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   147
                Transcript show:'q1 has data: '; show:q1 next; cr.
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   148
            ] ifFalse:[ 
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   149
                ret == q2 readSemaphore ifTrue:[
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   150
                    Transcript show:'q2 has data: '; show:q2 next; cr.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   151
                ] ifFalse:[
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1316
diff changeset
   152
                    Transcript showCR:'timeout'
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   153
                ]
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   154
            ].
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   155
        ] loop
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   156
    ] fork.
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   157
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   158
    (Delay forSeconds:3) wait.
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   159
    q1 nextPut:'one'.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   160
    (Delay forSeconds:2) wait.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   161
    q1 nextPut:'two'.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   162
    (Delay forSeconds:2) wait.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   163
    q1 nextPut:'three'.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   164
    (Delay forSeconds:6) wait.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   165
    proc terminate.
1316
248a8cb2ae3b examples
Claus Gittinger <cg@exept.de>
parents: 1294
diff changeset
   166
                                                                        [exEnd]
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   167
"
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   168
! !
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   169
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   170
!SemaphoreSet methodsFor:'wait'!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   171
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   172
wait
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   173
    "wait for any of the semaphores in the set to be signalled.
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   174
     Return the (first) semaphore which is triggered."
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   175
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   176
    |currentProcess gotSema wasBlocked registeredAllSemas|
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   177
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   178
    currentProcess := Processor activeProcess.
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   179
    registeredAllSemas := false.
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   180
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   181
    wasBlocked := OperatingSystem blockInterrupts.
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   182
    [
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   183
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   184
            gotSema := self detect:[:sema|
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   185
                sema checkAndRegisterProcess:currentProcess
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   186
            ] ifNone:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   187
                registeredAllSemas := true.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   188
                currentProcess suspendWithState:#wait.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   189
                nil
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   190
            ].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   191
        ] doWhile:[gotSema isNil].
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   192
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   193
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   194
          we finaly got at least one of our semaphores.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   195
          Now unregister from any semaphore, we are registered on.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   196
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   197
        self detect:[:sema|
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   198
            (sema == gotSema) ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   199
                registeredAllSemas not        
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   200
            ] ifFalse:[        
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   201
                sema unregisterProcess:currentProcess.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   202
                false
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   203
            ]
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   204
        ] ifNone:[].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   205
    ] valueNowOrOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   206
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
2876
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   207
        self do:[:aSema |
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   208
            aSema unregisterProcess:currentProcess.
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   209
        ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   210
    ].
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   211
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   212
    ^ gotSema
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   213
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   214
    "Modified: 15.12.1995 / 23:10:07 / stefan"
2876
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   215
    "Modified: 20.8.1997 / 18:33:09 / cg"
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   216
!
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   217
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   218
waitWithTimeout:seconds
770
402958905760 protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents: 769
diff changeset
   219
    "wait for any of the the semaphore, but abort the wait after some time.
775
7b6c00a4708a documentation / example methods merged into one (easier to browse for category "examples")
Claus Gittinger <cg@exept.de>
parents: 770
diff changeset
   220
     Return the (first) triggered semaphore if any, nil if we return due to a timeout."
770
402958905760 protocol of waitWithTImeOut: changed
Claus Gittinger <cg@exept.de>
parents: 769
diff changeset
   221
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   222
    |currentProcess gotSema wasBlocked now endTime unblock timeoutOccured registeredAllSemas|
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   223
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   224
    currentProcess := Processor activeProcess.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   225
    timeoutOccured := false.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   226
    registeredAllSemas := false.
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   227
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   228
    wasBlocked := OperatingSystem blockInterrupts.
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   229
    [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   230
        seconds notNil ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   231
            "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   232
             calculate the end-time
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   233
            "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   234
            now := OperatingSystem getMillisecondTime.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   235
            endTime := OperatingSystem millisecondTimeAdd:now and:(seconds * 1000).
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   236
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   237
            unblock := [timeoutOccured := true. Processor resume:currentProcess].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   238
            Processor addTimedBlock:unblock for:currentProcess atMilliseconds:endTime.
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   239
        ].
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   240
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   241
        [
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   242
            gotSema := self detect:[:sema|
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   243
                sema checkAndRegisterProcess:currentProcess
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   244
            ] ifNone:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   245
                registeredAllSemas := true.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   246
                currentProcess suspendWithState:#wait.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   247
                nil
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   248
            ].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   249
        ] doWhile:[gotSema isNil and:[timeoutOccured not]].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   250
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   251
        (timeoutOccured not and:[unblock notNil]) ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   252
            Processor removeTimedBlock:unblock.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   253
        ].
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   254
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   255
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   256
          we finaly got at least one of our semaphores.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   257
          Now unregister from any semaphore, we are registered on.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   258
        "
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   259
        self detect:[:sema|
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   260
            (sema == gotSema) ifTrue:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   261
                registeredAllSemas not        
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   262
            ] ifFalse:[        
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   263
                sema unregisterProcess:currentProcess.
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   264
                false
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   265
            ]
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   266
        ] ifNone:[].
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   267
    ] valueNowOrOnUnwindDo:[
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   268
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
2876
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   269
        self do:[:aSema |
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   270
            aSema unregisterProcess:currentProcess.
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   271
        ]
1031
cd715f8011f0 care to reenable interrupts
Claus Gittinger <cg@exept.de>
parents: 775
diff changeset
   272
    ].
769
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   273
    ^ gotSema
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   274
84cc1b36f27e Fix cleanup after process resume in SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents: 757
diff changeset
   275
    "Modified: 15.12.1995 / 23:10:54 / stefan"
2876
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   276
    "Modified: 20.8.1997 / 18:33:23 / cg"
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   277
! !
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   278
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   279
!SemaphoreSet class methodsFor:'documentation'!
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   280
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   281
version
2876
fb3fed7470be oops - must unregister from individual semaphores
Claus Gittinger <cg@exept.de>
parents: 1801
diff changeset
   282
    ^ '$Header: /cvs/stx/stx/libbasic/Attic/SemaSet.st,v 1.11 1997-08-20 16:39:12 cg Exp $'
757
93d5f6b86e98 Add SemaphoreSet.
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   283
! !