/* 
   multithreading.m

   Copyright (C) 1995, 1996, 1997 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <Foundation/NSObject.h>
#include <extensions/objc-runtime.h>

/*
 * Lock for printf
 */
 
static objc_mutex_t io_lock = NULL;
static int verbose = 0;

/*
 *   Condition lock
 *   The condition is the bitwise and of user value and condition's predicate
 */

@interface BitmapConditionLock : NSObject
{
    objc_mutex_t     mutex;
    objc_condition_t condition;
    volatile int      predicate;
}
- (id)initWithCondition:(int)value;
- (int)condition;
- (void)lockWhenCondition:(int)value;
- (void)unlockWithCondition:(int)value;
@end;

@implementation BitmapConditionLock

- (id)init
{
    [super init];
    mutex = objc_mutex_allocate();
    condition = objc_condition_allocate();
    predicate = 0;
    return self;
}

- (id)initWithCondition:(int)value
{
    [self init];
    predicate = value;
    return self;
}

- (void)dealloc
{
    objc_mutex_deallocate(mutex);
    objc_condition_deallocate(condition);
    [super dealloc];
}

- (int)condition
{
    return predicate;
}

- (void)lockWhenCondition:(int)value
{
    int depth;
    
    // Try to lock the mutex
    depth = objc_mutex_lock(mutex);

    // Return on error
    if (depth == -1)
	return;
	    
    // Error if recursive lock and condition is false 
    if ((depth > 1) && (predicate & value))
	return;

    // Wait condition
    while (!(predicate & value))
	objc_condition_wait(condition, mutex);
}

- (void)unlockWithCondition:(int)value
{
    int depth;
    
    // Try to lock the mutex again
    depth = objc_mutex_trylock(mutex);
    
    // Another thread has the lock so abort
    if (depth == -1)
	return;
    
    // If the depth is only 1 then we just acquired
    // the lock above, bogus unlock so abort
    if (depth == 1)
	return;
    
    // This is a valid unlock so set the condition
    // and unlock twice
    predicate = value;
    objc_condition_broadcast(condition);
    objc_mutex_unlock(mutex);
    objc_mutex_unlock(mutex);
}

@end

/*
*   Store Object
*/

@interface Store : NSObject
{
    BitmapConditionLock* lock;
    unsigned capacity;
    volatile int count;
}
- initWithCapacity:(unsigned)aCapacity;
- (int)addObject;
- (int)removeObject;
- (int)count;
@end;

typedef enum {
    StoreEmpty    = 1,
    StoreHasData  = 2,
    StoreFull     = 4
} StoreState;

@implementation Store

- initWithCapacity:(unsigned)aCapacity
{
    [super init];
    lock = [[BitmapConditionLock alloc] initWithCondition:StoreEmpty];
    capacity = aCapacity;
    count = 0;
    return self;
}

- (void)dealloc
{
    [lock release];
    [super dealloc];
}

- (int)addObject
{
    [lock lockWhenCondition:StoreEmpty|StoreHasData];
    count++;
    [lock unlockWithCondition:count<capacity ? StoreHasData : StoreFull];
    return count;
}

- (int)removeObject
{
    [lock lockWhenCondition:StoreFull|StoreHasData];
    count--;
    [lock unlockWithCondition:count ? StoreHasData : StoreEmpty];
    return count;
}

- (int)count
{
    return count;
}

@end;

/*
*   Producer/Consumer
*/

@interface Worker : NSObject
{
    int myId;
    Store* store;
    volatile BOOL done;
}
- initWithId:(int)anId store:aStore;
- (void)consume:(int)count;
- (void)produce:(int)count;
- (void)wait;
@end

@implementation Worker

- initWithId:(int)anId store:aStore 
{
    [super init];
    myId = anId;
    store = aStore;
    done = NO;
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)produce:(int)count
{
    int storeCount;
    
    if (verbose) {
	objc_mutex_lock(io_lock);
	printf("%2d: add: Start.\n", myId);
	objc_mutex_unlock(io_lock);
    }
    
    while (count>0) {
	storeCount = [store addObject];
	if (verbose) {
	    objc_mutex_lock(io_lock);
	    printf("%2d: add: %3d store has: %3d\n", myId, count, storeCount);
	    objc_mutex_unlock(io_lock);
	}
	count--;
    }
    if (verbose) {
	objc_mutex_lock(io_lock);
	printf("%2d: add: Done.\n", myId);
	objc_mutex_unlock(io_lock);
    }
    done = YES;
}

- (void)consume:(int)count
{
    int storeCount;

    if (verbose) {
	objc_mutex_lock(io_lock);
	printf("%2d: del: Start.\n", myId);
	objc_mutex_unlock(io_lock);
    }
    while (count>0) {
	storeCount = [store removeObject];
	if (verbose) {
	    objc_mutex_lock(io_lock);
	    printf("%2d: del: %3d store has: %3d\n", myId, count, storeCount);
	    objc_mutex_unlock(io_lock);
	}
	count--;
    }
    if (verbose) {
	objc_mutex_lock(io_lock);
	printf("%2d: del: Done.\n", myId);
	objc_mutex_unlock(io_lock);
    }
    done = YES;
}

- (void)wait
{
    while (!done);
}

@end

@interface TestThreads : NSObject
+ (BOOL)runBitmapConditionTest:(int)sc 
  producers:(int)np items:(int)pi 
  consumers:(int)nc items:(int)ci;
@end

@implementation TestThreads

int items_to_consume, items_to_produce;

static void consumeFunc(id obj)
{
    [obj consume:items_to_consume];
}

static void produceFunc(id obj)
{
    [obj produce:items_to_produce];
}

+ (BOOL)runBitmapConditionTest:(int)sc 
  producers:(int)np items:(int)pi 
  consumers:(int)nc items:(int)ci
{
    int nprod  = np;
    int ncons  = nc;
    int scap   = sc;
    int pitems = pi;
    int citems = ci;
    
    id store, producers[nprod], consumers[ncons];
    int i;

    printf("Starting test: store capacity %d "
	    "\n\tproducers %d each %d items, "
	    "\n\tconsumers %d each %d items\n",
	    scap, nprod, pitems, ncons, citems);

    if (nprod*pitems != ncons*citems) {
	printf("Error: (producers*prod_items) "
		"must be equal to (consumers*cons_items)\n");
	return 0;
    }

    io_lock = objc_mutex_allocate();

    store = [[Store alloc] initWithCapacity:scap];
    
    for (i=0; i<ncons; i++)
	consumers[i] = [[Worker alloc] initWithId:i store:store];
    for (i=0; i<nprod; i++)
	producers[i] = [[Worker alloc] initWithId:i store:store];

    items_to_consume = citems;
    printf("Starting consumers.\n");
    for (i=0; i<ncons; i++)
	objc_thread_create((void (*)(void *arg))consumeFunc, consumers[i]);
	//objc_thread_detach(@selector(consume:), consumers[i], (id)citems);

    items_to_produce = pitems;
    printf("Starting producers.\n");
    for (i=0; i<nprod; i++)
	objc_thread_create((void (*)(void *arg))produceFunc, producers[i]);
	//objc_thread_create(@selector(produce:), producers[i], (id)pitems);

    for (i=0; i<ncons; i++) {
	[consumers[i] wait];
	[consumers[i] release];
    }
    for (i=0; i<nprod; i++) {
	[producers[i] wait];
	[producers[i] release];
    }

    [store release];

    printf("thread test Ok.\n");
    return 1;
}

@end
