Home | History | Annotate | Line # | Download | only in raidframe
rf_mcpair.c revision 1.1
      1  1.1  oster /*	$NetBSD: rf_mcpair.c,v 1.1 1998/11/13 04:20:31 oster Exp $	*/
      2  1.1  oster /*
      3  1.1  oster  * Copyright (c) 1995 Carnegie-Mellon University.
      4  1.1  oster  * All rights reserved.
      5  1.1  oster  *
      6  1.1  oster  * Author: Jim Zelenka
      7  1.1  oster  *
      8  1.1  oster  * Permission to use, copy, modify and distribute this software and
      9  1.1  oster  * its documentation is hereby granted, provided that both the copyright
     10  1.1  oster  * notice and this permission notice appear in all copies of the
     11  1.1  oster  * software, derivative works or modified versions, and any portions
     12  1.1  oster  * thereof, and that both notices appear in supporting documentation.
     13  1.1  oster  *
     14  1.1  oster  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  1.1  oster  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  1.1  oster  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  1.1  oster  *
     18  1.1  oster  * Carnegie Mellon requests users of this software to return to
     19  1.1  oster  *
     20  1.1  oster  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  1.1  oster  *  School of Computer Science
     22  1.1  oster  *  Carnegie Mellon University
     23  1.1  oster  *  Pittsburgh PA 15213-3890
     24  1.1  oster  *
     25  1.1  oster  * any improvements or extensions that they make and grant Carnegie the
     26  1.1  oster  * rights to redistribute these changes.
     27  1.1  oster  */
     28  1.1  oster 
     29  1.1  oster /* rf_mcpair.c
     30  1.1  oster  * an mcpair is a structure containing a mutex and a condition variable.
     31  1.1  oster  * it's used to block the current thread until some event occurs.
     32  1.1  oster  */
     33  1.1  oster 
     34  1.1  oster /* :
     35  1.1  oster  * Log: rf_mcpair.c,v
     36  1.1  oster  * Revision 1.16  1996/06/19 22:23:01  jimz
     37  1.1  oster  * parity verification is now a layout-configurable thing
     38  1.1  oster  * not all layouts currently support it (correctly, anyway)
     39  1.1  oster  *
     40  1.1  oster  * Revision 1.15  1996/06/17  03:18:04  jimz
     41  1.1  oster  * include shutdown.h for macroized ShutdownCreate
     42  1.1  oster  *
     43  1.1  oster  * Revision 1.14  1996/06/10  11:55:47  jimz
     44  1.1  oster  * Straightened out some per-array/not-per-array distinctions, fixed
     45  1.1  oster  * a couple bugs related to confusion. Added shutdown lists. Removed
     46  1.1  oster  * layout shutdown function (now subsumed by shutdown lists).
     47  1.1  oster  *
     48  1.1  oster  * Revision 1.13  1996/06/05  18:06:02  jimz
     49  1.1  oster  * Major code cleanup. The Great Renaming is now done.
     50  1.1  oster  * Better modularity. Better typing. Fixed a bunch of
     51  1.1  oster  * synchronization bugs. Made a lot of global stuff
     52  1.1  oster  * per-desc or per-array. Removed dead code.
     53  1.1  oster  *
     54  1.1  oster  * Revision 1.12  1996/06/02  17:31:48  jimz
     55  1.1  oster  * Moved a lot of global stuff into array structure, where it belongs.
     56  1.1  oster  * Fixed up paritylogging, pss modules in this manner. Some general
     57  1.1  oster  * code cleanup. Removed lots of dead code, some dead files.
     58  1.1  oster  *
     59  1.1  oster  * Revision 1.11  1996/05/30  23:22:16  jimz
     60  1.1  oster  * bugfixes of serialization, timing problems
     61  1.1  oster  * more cleanup
     62  1.1  oster  *
     63  1.1  oster  * Revision 1.10  1996/05/20  16:15:22  jimz
     64  1.1  oster  * switch to rf_{mutex,cond}_{init,destroy}
     65  1.1  oster  *
     66  1.1  oster  * Revision 1.9  1996/05/18  19:51:34  jimz
     67  1.1  oster  * major code cleanup- fix syntax, make some types consistent,
     68  1.1  oster  * add prototypes, clean out dead code, et cetera
     69  1.1  oster  *
     70  1.1  oster  * Revision 1.8  1996/05/16  16:04:42  jimz
     71  1.1  oster  * convert to return-val on FREELIST init
     72  1.1  oster  *
     73  1.1  oster  * Revision 1.7  1996/05/16  14:47:21  jimz
     74  1.1  oster  * rewrote to use RF_FREELIST
     75  1.1  oster  *
     76  1.1  oster  * Revision 1.6  1995/12/01  19:25:43  root
     77  1.1  oster  * added copyright info
     78  1.1  oster  *
     79  1.1  oster  */
     80  1.1  oster 
     81  1.1  oster #include "rf_types.h"
     82  1.1  oster #include "rf_threadstuff.h"
     83  1.1  oster #include "rf_mcpair.h"
     84  1.1  oster #include "rf_debugMem.h"
     85  1.1  oster #include "rf_freelist.h"
     86  1.1  oster #include "rf_shutdown.h"
     87  1.1  oster 
     88  1.1  oster #if defined(__NetBSD__) && defined(_KERNEL)
     89  1.1  oster #include <sys/proc.h>
     90  1.1  oster 
     91  1.1  oster #endif
     92  1.1  oster 
     93  1.1  oster static RF_FreeList_t *rf_mcpair_freelist;
     94  1.1  oster 
     95  1.1  oster #define RF_MAX_FREE_MCPAIR 128
     96  1.1  oster #define RF_MCPAIR_INC       16
     97  1.1  oster #define RF_MCPAIR_INITIAL   24
     98  1.1  oster 
     99  1.1  oster static int init_mcpair(RF_MCPair_t *);
    100  1.1  oster static void clean_mcpair(RF_MCPair_t *);
    101  1.1  oster static void rf_ShutdownMCPair(void *);
    102  1.1  oster 
    103  1.1  oster 
    104  1.1  oster 
    105  1.1  oster static int init_mcpair(t)
    106  1.1  oster   RF_MCPair_t  *t;
    107  1.1  oster {
    108  1.1  oster 	int rc;
    109  1.1  oster 
    110  1.1  oster 	rc = rf_mutex_init(&t->mutex);
    111  1.1  oster 	if (rc) {
    112  1.1  oster 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    113  1.1  oster 			__LINE__, rc);
    114  1.1  oster 		return(rc);
    115  1.1  oster 	}
    116  1.1  oster 	rc = rf_cond_init(&t->cond);
    117  1.1  oster 	if (rc) {
    118  1.1  oster 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
    119  1.1  oster 			__LINE__, rc);
    120  1.1  oster 		rf_mutex_destroy(&t->mutex);
    121  1.1  oster 		return(rc);
    122  1.1  oster 	}
    123  1.1  oster 	return(0);
    124  1.1  oster }
    125  1.1  oster 
    126  1.1  oster static void clean_mcpair(t)
    127  1.1  oster   RF_MCPair_t  *t;
    128  1.1  oster {
    129  1.1  oster 	rf_mutex_destroy(&t->mutex);
    130  1.1  oster 	rf_cond_destroy(&t->cond);
    131  1.1  oster }
    132  1.1  oster 
    133  1.1  oster static void rf_ShutdownMCPair(ignored)
    134  1.1  oster   void  *ignored;
    135  1.1  oster {
    136  1.1  oster 	RF_FREELIST_DESTROY_CLEAN(rf_mcpair_freelist,next,(RF_MCPair_t *),clean_mcpair);
    137  1.1  oster }
    138  1.1  oster 
    139  1.1  oster int rf_ConfigureMCPair(listp)
    140  1.1  oster   RF_ShutdownList_t  **listp;
    141  1.1  oster {
    142  1.1  oster 	int rc;
    143  1.1  oster 
    144  1.1  oster 	RF_FREELIST_CREATE(rf_mcpair_freelist, RF_MAX_FREE_MCPAIR,
    145  1.1  oster 		RF_MCPAIR_INC, sizeof(RF_MCPair_t));
    146  1.1  oster 	rc = rf_ShutdownCreate(listp, rf_ShutdownMCPair, NULL);
    147  1.1  oster 	if (rc) {
    148  1.1  oster 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    149  1.1  oster 			__FILE__, __LINE__, rc);
    150  1.1  oster 		rf_ShutdownMCPair(NULL);
    151  1.1  oster 		return(rc);
    152  1.1  oster 	}
    153  1.1  oster 	RF_FREELIST_PRIME_INIT(rf_mcpair_freelist, RF_MCPAIR_INITIAL,next,
    154  1.1  oster 		(RF_MCPair_t *),init_mcpair);
    155  1.1  oster 	return(0);
    156  1.1  oster }
    157  1.1  oster 
    158  1.1  oster RF_MCPair_t *rf_AllocMCPair()
    159  1.1  oster {
    160  1.1  oster 	RF_MCPair_t *t;
    161  1.1  oster 
    162  1.1  oster 	RF_FREELIST_GET_INIT(rf_mcpair_freelist,t,next,(RF_MCPair_t *),init_mcpair);
    163  1.1  oster 	if (t) {
    164  1.1  oster 		t->flag = 0;
    165  1.1  oster 		t->next = NULL;
    166  1.1  oster 	}
    167  1.1  oster 	return(t);
    168  1.1  oster }
    169  1.1  oster 
    170  1.1  oster void rf_FreeMCPair(t)
    171  1.1  oster   RF_MCPair_t   *t;
    172  1.1  oster {
    173  1.1  oster 	RF_FREELIST_FREE_CLEAN(rf_mcpair_freelist,t,next,clean_mcpair);
    174  1.1  oster }
    175  1.1  oster 
    176  1.1  oster /* the callback function used to wake you up when you use an mcpair to wait for something */
    177  1.1  oster void rf_MCPairWakeupFunc(mcpair)
    178  1.1  oster   RF_MCPair_t  *mcpair;
    179  1.1  oster {
    180  1.1  oster 	RF_LOCK_MUTEX(mcpair->mutex);
    181  1.1  oster 	mcpair->flag = 1;
    182  1.1  oster #if 0
    183  1.1  oster printf("MCPairWakeupFunc called!\n");
    184  1.1  oster #endif
    185  1.1  oster #ifdef KERNEL
    186  1.1  oster 	wakeup(&(mcpair->flag)); /* XXX Does this do anything useful!! GO */
    187  1.1  oster 	/* XXX Looks like the following is needed to truly get the
    188  1.1  oster functionality they were looking for here... This could be a side-effect
    189  1.1  oster of my using a tsleep in the NetBSD port though... XXX */
    190  1.1  oster #if defined(__NetBSD__) && defined(_KERNEL)
    191  1.1  oster 	wakeup(&(mcpair->cond)); /* XXX XXX XXX GO */
    192  1.1  oster #endif
    193  1.1  oster #else /* KERNEL */
    194  1.1  oster 	RF_SIGNAL_COND(mcpair->cond);
    195  1.1  oster #endif /* KERNEL */
    196  1.1  oster 	RF_UNLOCK_MUTEX(mcpair->mutex);
    197  1.1  oster }
    198