UnixOperatingSystem.st
changeset 6340 65183f0db451
parent 6334 cbb0e8c8c1a5
child 6343 3e35a55af7c5
equal deleted inserted replaced
6339:5b07b084a2fc 6340:65183f0db451
  3553     if (fd < 0) {
  3553     if (fd < 0) {
  3554         if (errno == EINTR) {
  3554         if (errno == EINTR) {
  3555             __HANDLE_INTERRUPTS__;
  3555             __HANDLE_INTERRUPTS__;
  3556             goto again;
  3556             goto again;
  3557         } else {
  3557         } else {
  3558             error = __MKSMALLINT(errno);
  3558             error = __mkSmallInteger(errno);
  3559             goto err;
  3559             goto err;
  3560         }
  3560         }
  3561     }
  3561     }
  3562     fileDescriptor = __MKSMALLINT(fd);
  3562     fileDescriptor = __mkSmallInteger(fd);
  3563 err:;
  3563 err:;
  3564 %}.
  3564 %}.
  3565     ^ fileDescriptor notNil ifTrue:[
  3565     ^ fileDescriptor notNil ifTrue:[
  3566         FileDescriptorHandle for:fileDescriptor.
  3566         FileDescriptorHandle for:fileDescriptor.
  3567     ] ifFalse:[
  3567     ] ifFalse:[
  9282     "
  9282     "
  9283 ! !
  9283 ! !
  9284 
  9284 
  9285 !UnixOperatingSystem::FileDescriptorHandle methodsFor:'misc functions'!
  9285 !UnixOperatingSystem::FileDescriptorHandle methodsFor:'misc functions'!
  9286 
  9286 
  9287 readCheck
  9287 canReadWithoutBlocking
  9288     "return true, if data is available on a filedescriptor 
  9288     "return true, if data is available on a filedescriptor 
  9289      (i.e. read is possible without blocking).
  9289      (i.e. read is possible without blocking).
  9290      This depends on a working select or FIONREAD to be provided by the OS."
  9290      This depends on a working select or FIONREAD to be provided by the OS."
  9291 
  9291 
  9292 %{
  9292 %{
  9347         "/ effectively polling for input.
  9347         "/ effectively polling for input.
  9348 
  9348 
  9349         ^ true
  9349         ^ true
  9350     ].
  9350     ].
  9351 
  9351 
  9352     (OperatingSystem selectOnAnyReadable:(Array with:fd)
  9352     ^ (OperatingSystem selectOnAnyReadable:(Array with:fd)
  9353                      writable:nil
  9353                        writable:nil
  9354                     exception:nil
  9354                        exception:nil
  9355                   withTimeOut:0) == fd
  9355                        withTimeOut:0) == fd
  9356         ifTrue:[^ true].
  9356 !
  9357     ^ false
  9357 
       
  9358 seekTo:newPosition from:whence
       
  9359     "seek to newPosition
       
  9360      whence is one of: #begin #current #end.
       
  9361      Return the new position.
       
  9362 
       
  9363      TODO: 64 bit handling"
       
  9364 
       
  9365     |error|
       
  9366 
       
  9367 %{
       
  9368     INT fd, pos, ret;
       
  9369     INT __whence;
       
  9370     __uint64__ pos64;
       
  9371 
       
  9372     if (! __isSmallInteger(__INST(fd))) {
       
  9373         error = @symbol(errorNotOpen);
       
  9374         goto bad;
       
  9375     }
       
  9376     if (__isSmallInteger(newPosition)) {
       
  9377         pos = __smallIntegerVal(newPosition);
       
  9378     } else if (__signedLong64IntVal(newPosition, &pos64) == 0) {
       
  9379         error = @symbol(badArgument1);
       
  9380         goto bad;
       
  9381     }
       
  9382     fd = __smallIntegerVal(__INST(fd));
       
  9383     if (fd < 0) {
       
  9384         error = @symbol(internalError);
       
  9385         goto bad;
       
  9386     }
       
  9387     if (whence == @symbol(begin)) {
       
  9388         __whence = SEEK_SET;    
       
  9389     } else if (whence == @symbol(current)) {
       
  9390         __whence = SEEK_CUR;    
       
  9391     } else if (whence == @symbol(end)) {
       
  9392         __whence = SEEK_END;    
       
  9393     } else {
       
  9394         error = @symbol(badArgument2);
       
  9395         goto bad;
       
  9396     }
       
  9397 
       
  9398 again:
       
  9399     ret = lseek(fd, pos, __whence);
       
  9400     if (ret < 0) {
       
  9401         if (errno == EINTR) {
       
  9402             __HANDLE_INTERRUPTS__;
       
  9403             goto again;
       
  9404         }
       
  9405         error = __mkSmallInteger(errno);
       
  9406         goto bad;
       
  9407     }
       
  9408     /* RETURN (__mkInteger(ret)); */
       
  9409     RETURN (__mkSmallInteger(ret));
       
  9410 
       
  9411 bad: ;   
       
  9412 %}.
       
  9413     ^ self error:error.
       
  9414 
       
  9415     "
       
  9416      |h buff n|
       
  9417 
       
  9418      h := OperatingSystem openFileForRead:'/etc/hosts'.
       
  9419      h seekTo:10 from:#begin.
       
  9420      buff := ByteArray new:1000. buff inspect.
       
  9421      n := h readBytes:1000 into:buff startingAt:1.
       
  9422      Transcript show:n; space; showCR:buff asString.
       
  9423     "
  9358 !
  9424 !
  9359 
  9425 
  9360 selectWithTimeOut:millis
  9426 selectWithTimeOut:millis
  9361     "wait for aFileDesriptor to become ready; timeout after t milliseconds.
  9427     "wait for aFileDesriptor to become ready; timeout after t milliseconds.
  9362      Return true, if i/o ok, false if timed-out or interrupted.
  9428      Return true, if i/o ok, false if timed-out or interrupted.
  9419      fd argument not integer
  9485      fd argument not integer
  9420     "
  9486     "
  9421     ^ self primitiveFailed
  9487     ^ self primitiveFailed
  9422 
  9488 
  9423 
  9489 
  9424 !
  9490 ! !
  9425 
  9491 
  9426 writeCheck
  9492 !UnixOperatingSystem::FileDescriptorHandle methodsFor:'private accessing'!
       
  9493 
       
  9494 setFileDescriptor:anInteger
       
  9495 
       
  9496 %{
       
  9497     if (__isSmallInteger(anInteger)) {
       
  9498         __externalAddressVal(self) = (OBJ)(__smallIntegerVal(anInteger));
       
  9499     }
       
  9500 %}
       
  9501 
       
  9502 
       
  9503 ! !
       
  9504 
       
  9505 !UnixOperatingSystem::FileDescriptorHandle methodsFor:'queries'!
       
  9506 
       
  9507 canWriteWithoutBlocking
  9427     "return true, if filedescriptor can be written without blocking"
  9508     "return true, if filedescriptor can be written without blocking"
  9428 
  9509 
  9429     OperatingSystem supportsSelect ifFalse:[
  9510     OperatingSystem supportsSelect ifFalse:[
  9430         "/ mhmh - what should we do then ?
  9511         "/ mhmh - what should we do then ?
  9431         "/ For now, return true as if data was present,
  9512         "/ For now, return true as if data was present,
  9433         "/ It will then (hopefully) be desceduled there and
  9514         "/ It will then (hopefully) be desceduled there and
  9434         "/ effectively polling for output.
  9515         "/ effectively polling for output.
  9435         ^ true
  9516         ^ true
  9436     ].
  9517     ].
  9437 
  9518 
  9438     (OperatingSystem selectOnAnyReadable:nil
  9519     ^ (OperatingSystem selectOnAnyReadable:nil
  9439                      writable:(Array with:fd)
  9520                        writable:(Array with:fd)
  9440                     exception:nil
  9521                        exception:nil
  9441                   withTimeOut:0) == fd
  9522                        withTimeOut:0) == fd
  9442         ifTrue:[^ true].
  9523 !
  9443     ^ false
       
  9444 
       
  9445     "Created: 1.10.1997 / 08:49:24 / stefan"
       
  9446 ! !
       
  9447 
       
  9448 !UnixOperatingSystem::FileDescriptorHandle methodsFor:'private accessing'!
       
  9449 
       
  9450 setFileDescriptor:anInteger
       
  9451 
       
  9452 %{
       
  9453     if (__isSmallInteger(anInteger)) {
       
  9454         __externalAddressVal(self) = (OBJ)(__smallIntegerVal(anInteger));
       
  9455     }
       
  9456 %}
       
  9457 
       
  9458 
       
  9459 ! !
       
  9460 
       
  9461 !UnixOperatingSystem::FileDescriptorHandle methodsFor:'queries'!
       
  9462 
  9524 
  9463 isValid
  9525 isValid
  9464     "answer true, if the handle is valid, i.e. connected to
  9526     "answer true, if the handle is valid, i.e. connected to
  9465      a file or some other OS object"
  9527      a file or some other OS object"
  9466 
  9528 
  9504 
  9566 
  9505     "Created: 30.9.1997 / 12:37:26 / stefan"
  9567     "Created: 30.9.1997 / 12:37:26 / stefan"
  9506     "Modified: 30.9.1997 / 12:42:16 / stefan"
  9568     "Modified: 30.9.1997 / 12:42:16 / stefan"
  9507 ! !
  9569 ! !
  9508 
  9570 
       
  9571 !UnixOperatingSystem::FileDescriptorHandle methodsFor:'waiting'!
       
  9572 
       
  9573 readWaitWithTimeoutMs:timeout 
       
  9574     "suspend the current process, until the receiver
       
  9575      becomes ready for reading or a timeout (in milliseconds) expired. 
       
  9576      If data is already available, return immediate. 
       
  9577      Return true if a timeout occured (i.e. false, if data is available).
       
  9578      The other threads are not affected by the wait."
       
  9579 
       
  9580     |inputSema hasData wasBlocked|
       
  9581 
       
  9582     fd isNil ifTrue:[^ self error:#errorNotOpen].
       
  9583 
       
  9584     wasBlocked := OperatingSystem blockInterrupts.
       
  9585     hasData := self canReadWithoutBlocking.
       
  9586     hasData ifFalse:[
       
  9587         inputSema := Semaphore new name:'readWait'.
       
  9588         [
       
  9589             timeout notNil ifTrue:[
       
  9590                 Processor signal:inputSema afterMilliseconds:timeout 
       
  9591             ].
       
  9592             Processor signal:inputSema onInput:fd.
       
  9593             Processor activeProcess state:#ioWait.
       
  9594             inputSema wait.
       
  9595             Processor disableSemaphore:inputSema.
       
  9596             hasData := self canReadWithoutBlocking.
       
  9597         ] ifCurtailed:[
       
  9598             Processor disableSemaphore:inputSema.
       
  9599             wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
       
  9600         ]
       
  9601     ].
       
  9602     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
       
  9603     ^ hasData not
       
  9604 !
       
  9605 
       
  9606 writeWaitWithTimeoutMs:timeout
       
  9607     "suspend the current process, until the receiver
       
  9608      becomes ready for writing or a timeout (in seconds) expired. 
       
  9609      Return true if a timeout occured (i.e. false, if data is available).
       
  9610      Return immediate if the receiver is already ready. 
       
  9611      The other threads are not affected by the wait."
       
  9612 
       
  9613     |fd outputSema canWrite wasBlocked|
       
  9614 
       
  9615     fd isNil ifTrue:[^ self error:#errorNotOpen].
       
  9616 
       
  9617     wasBlocked := OperatingSystem blockInterrupts.
       
  9618     canWrite := self canWriteWithoutBlocking.
       
  9619     canWrite ifFalse:[
       
  9620         outputSema := Semaphore new name:'writeWait'.
       
  9621         [
       
  9622             timeout notNil ifTrue:[
       
  9623                 Processor signal:outputSema afterMilliseconds:timeout
       
  9624             ].
       
  9625             Processor signal:outputSema onOutput:fd.
       
  9626             Processor activeProcess state:#ioWait.
       
  9627             outputSema wait.
       
  9628             Processor disableSemaphore:outputSema.
       
  9629             canWrite := self canWriteWithoutBlocking.
       
  9630         ] ifCurtailed:[
       
  9631             Processor disableSemaphore:outputSema.
       
  9632             wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
       
  9633         ]
       
  9634     ].
       
  9635     wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
       
  9636     ^ canWrite not
       
  9637 ! !
       
  9638 
  9509 !UnixOperatingSystem::FilePointerHandle class methodsFor:'documentation'!
  9639 !UnixOperatingSystem::FilePointerHandle class methodsFor:'documentation'!
  9510 
  9640 
  9511 version
  9641 version
  9512     ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.105 2001-12-18 13:12:38 stefan Exp $'
  9642     ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.106 2001-12-19 12:01:26 stefan Exp $'
  9513 ! !
  9643 ! !
  9514 
  9644 
  9515 !UnixOperatingSystem::FilePointerHandle methodsFor:'release'!
  9645 !UnixOperatingSystem::FilePointerHandle methodsFor:'release'!
  9516 
  9646 
  9517 closeFile
  9647 closeFile
  9819 ! !
  9949 ! !
  9820 
  9950 
  9821 !UnixOperatingSystem class methodsFor:'documentation'!
  9951 !UnixOperatingSystem class methodsFor:'documentation'!
  9822 
  9952 
  9823 version
  9953 version
  9824     ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.105 2001-12-18 13:12:38 stefan Exp $'
  9954     ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.106 2001-12-19 12:01:26 stefan Exp $'
  9825 ! !
  9955 ! !
  9826 UnixOperatingSystem initialize!
  9956 UnixOperatingSystem initialize!