home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Apps / ArchiveUtils / Freeze / thread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-20  |  3.2 KB  |  115 lines

  1. /*
  2.  *    Filename:    thread.c 
  3.  *    Created :    Sat Dec 21 00:24:36 1991 
  4.  *    Author  :    Vince DeMarco
  5.  *        <vince@whatnxt.cuc.ab.ca>
  6.  */
  7.  
  8. /* 
  9.    Copyright (C) 1990 Free Software Foundation, Inc.
  10.  
  11.    This program is free software; you can redistribute it and/or modify
  12.    it under the terms of the GNU General Public License as published by
  13.    the Free Software Foundation; either version 2, or (at your option)
  14.    any later version.
  15.  
  16.    This program is distributed in the hope that it will be useful,
  17.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.    GNU General Public License for more details.
  20.  
  21.    You should have received a copy of the GNU General Public License
  22.    along with this program; if not, write to the Free Software
  23.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <strings.h>
  28.  
  29. #define THREAD
  30. #include "thread.h"
  31. #include "Freeze.h"
  32.  
  33. void buffer_put ( char *ch, buffer_t *buffer)
  34. {
  35.     mutex_lock(&buffer->lock);
  36.     /* Enter Critical Section */
  37.     while (buffer->count == BUFFER_SIZE){ /* Check if buffer is full, if it is wait
  38.                        * until it has some space in it
  39.                        */
  40.     condition_wait(&buffer->not_full, &buffer->lock);
  41.     }
  42.     ASSERT(0 <= buffer->count && buffer->count < BUFFER_SIZE);
  43.     strncpy(buffer->buf[buffer->start],ch,BUFFER_WIDTH);
  44.     buffer->start = (buffer->start + 1) % BUFFER_SIZE;
  45.     buffer->count++;
  46.     condition_signal(&buffer->not_empty);
  47.     /* Exit Critical Section */
  48.     mutex_unlock(&buffer->lock);
  49. }
  50.  
  51. void buffer_get ( buffer_t *buffer, char *ch )
  52. {
  53.     mutex_lock(&buffer->lock);
  54.     /* Enter Critical Section */
  55.     while (buffer->count == 0){ /* Check if the buffer is empty, if it is 
  56.                  * wait until somestuff has been placed in
  57.                  * the buffer
  58.                  */
  59.     condition_wait(&buffer->not_empty, &buffer->lock);
  60.     }
  61.     strcpy(ch,buffer->buf[buffer->end]);
  62.     buffer->buf[buffer->end][0] = '\000';
  63.     buffer->end = (buffer->end + 1) % BUFFER_SIZE;
  64.     buffer->count--;
  65.     condition_signal(&buffer->not_full);
  66.     /* Exit Critical Section */
  67.     mutex_unlock(&buffer->lock);
  68. }
  69.  
  70. /* Consumer process ie: background thread */
  71. void consumer(buffer_t *buffer)
  72. {
  73.     char current[BUFFER_WIDTH];
  74.     char *temp;
  75.     int  err_ret;
  76.  
  77.     while(1){
  78.     buffer_get(buffer,current);
  79.     temp = rindex(current,'.');
  80.  
  81.     /* Add check of Defaults database to see if the user
  82.      * want to use greedy Freeze
  83.      */
  84.     if ((temp != NULL) && (!strcmp(temp,".F"))) {
  85.         err_ret = Freeze( (FREEZE_FORCE | FREEZE_MELT_FILE),current);
  86.     }else{
  87.         err_ret = Freeze( (FREEZE_FORCE | FREEZE_FREZ_FILE),current);
  88.     }
  89.     switch (err_ret) { /* Since the Appkit isn't thread safe i can't print
  90.                 * out any errors if err_ret != FREEZE_OKAY
  91.                 * maybe when v3.0 is out.
  92.                 */
  93.         case FREEZE_FILEGONE :
  94.           break;
  95.         case FREEZE_NOFREEZE :
  96.           break;
  97.         case FREEZE_ALREADY  :
  98.           break;
  99.         case FREEZE_NOSAVINGS:
  100.           break;
  101.         case FREEZE_NOTFROZEN:
  102.           break;
  103.         case FREEZE_NOTREG   :
  104.         break;
  105.             case FREEZE_OTHERLINK:
  106.           break;
  107.         case FREEZE_CANTOPEN :
  108.         break;
  109.         case FREEZE_OKAY     :
  110.         default :
  111.             break;
  112.         }
  113.     }
  114. }
  115.