}
// Add item to a list of inventory
- public int additem(String name, int quantity, int price){
+ public synchronized int additem(String name, int quantity, int price){
ItemInfo newitem = new ItemInfo(quantity, price);
// Get the item from hash
if (map.containsKey(name) == false) {
}
// Buy item from a given list of inventory
- public int buyitem(String name, int quantity){
+ public synchronized int buyitem(String name, int quantity){
if (map.containsKey(name) == false) {
// System.printString("Error - Item does not exist");
return -1;
}
//Display the inventory list
- public String inventory(){
+ public synchronized String inventory(){
HashMapIterator i = new HashMapIterator(map, 0);// Gets key from the hashmap= name of item
HashMapIterator j = new HashMapIterator(map, 1);//Gets the value from hashmap
StringBuffer sb = new StringBuffer("");
public class Object {
public native int hashCode();
+ private Object nextlockobject;
+ private Object prevlockobject;
+
/* DON'T USE THIS METHOD UNLESS NECESSARY */
/* WE WILL DEPRECATE IT AS SOON AS INSTANCEOF WORKS */
outclassdefs.println(" int type;");
if (state.THREAD) {
outclassdefs.println(" pthread_t tid;");
+ outclassdefs.println(" void * lockentry;");
outclassdefs.println(" int lockcount;");
}
classdefout.println(" int type;");
if (state.THREAD) {
classdefout.println(" pthread_t tid;");
+ classdefout.println(" void * lockentry;");
classdefout.println(" int lockcount;");
}
#ifdef THREADS
/* Go to next thread */
if (listptr!=NULL) {
+ void * orig=listptr->locklist;
+ void * copy;
+ if (gc_createcopy(orig,©))
+ enqueue(orig);
+ listptr->locklist=copy;
stackptr=listptr->stackptr;
listptr=listptr->next;
}
struct listitem * stopforgc(struct garbagelist * ptr) {
struct listitem * litem=malloc(sizeof(struct listitem));
litem->stackptr=ptr;
+ litem->locklist=pthread_getspecific(threadlocks);
litem->prev=NULL;
pthread_mutex_lock(&gclistlock);
litem->next=list;
void restartaftergc(struct listitem * litem) {
pthread_mutex_lock(&gclistlock);
+ pthread_setspecific(threadlocks, litem->locklist);
if (litem->prev==NULL) {
list=litem->next;
} else {
struct listitem * prev;
struct listitem * next;
struct garbagelist * stackptr;
+ struct ___Object___ * locklist;
};
#ifdef THREADS
#endif
while(1) {
if (VAR(___this___)->tid==0) {
+ VAR(___this___)->___prevlockobject___=NULL;
+ VAR(___this___)->___nextlockobject___=(struct ___Object___ *)pthread_getspecific(threadlocks);
+ VAR(___this___)->___nextlockobject___->___prevlockobject___=VAR(___this___);
+ pthread_setspecific(threadlocks, VAR(___this___));
VAR(___this___)->lockcount=1;
VAR(___this___)->tid=self;
pthread_mutex_unlock(&objlock);
pthread_t self=pthread_self();
if (self==VAR(___this___)->tid) {
VAR(___this___)->lockcount--;
- if (VAR(___this___)->lockcount==0)
+ if (VAR(___this___)->lockcount==0) {
+ if (VAR(___this___)->___prevlockobject___==NULL) {
+ pthread_setspecific(threadlocks, VAR(___this___)->___nextlockobject___);
+ } else
+ VAR(___this___)->___prevlockobject___->___nextlockobject___=VAR(___this___)->___nextlockobject___;
+ if (VAR(___this___)->___nextlockobject___!=NULL)
+ VAR(___this___)->___nextlockobject___->___prevlockobject___=VAR(___this___)->___prevlockobject___;
+ VAR(___this___)->lockentry=NULL;
VAR(___this___)->tid=0;
+ }
pthread_mutex_lock(&objlock);
pthread_cond_broadcast(&objcond);
pthread_mutex_unlock(&objlock);
v->type=type;
#ifdef THREADS
v->tid=0;
+ v->lockentry=0;
v->lockcount=0;
#endif
return v;
v->___length___=length;
#ifdef THREADS
v->tid=0;
+ v->lockentry=0;
v->lockcount=0;
#endif
return v;
void failedboundschk() {
#ifndef TASK
printf("Array out of bounds\n");
+#ifdef THREADS
+ threadexit();
+#else
exit(-1);
+#endif
#else
longjmp(error_handler,2);
#endif
pthread_cond_t gccond;
pthread_mutex_t objlock;
pthread_cond_t objcond;
+pthread_key_t threadlocks;
+
+void threadexit() {
+ struct ___Object___ *ll=pthread_getspecific(threadlocks);
+ while(ll!=NULL) {
+ struct ___Object___ *llnext=ll->___nextlockobject___;
+ ll->___nextlockobject___=NULL;
+ ll->___prevlockobject___=NULL;
+ ll->lockcount=0;
+ ll->tid=0; //unlock it
+ ll=llnext;
+ }
+ pthread_mutex_lock(&objlock);//wake everyone up
+ pthread_cond_broadcast(&objcond);
+ pthread_mutex_unlock(&objlock);
+ pthread_exit(NULL);
+}
+
+void threadhandler(int sig, siginfo_t *info, void *uap) {
+#ifdef DEBUG
+ printf("sig=%d\n",sig);
+ printf("signal\n");
+#endif
+ threadexit();
+}
void initializethreads() {
+ struct sigaction sig;
threadcount=1;
pthread_mutex_init(&gclock, NULL);
pthread_mutex_init(&gclistlock, NULL);
pthread_cond_init(&gccond, NULL);
pthread_mutex_init(&objlock,NULL);
pthread_cond_init(&objcond,NULL);
+ pthread_key_create(&threadlocks, NULL);
+
+ sig.sa_sigaction=&threadhandler;
+ sig.sa_flags=SA_SIGINFO;
+ sigemptyset(&sig.sa_mask);
+
+ /* Catch bus errors, segmentation faults, and floating point exceptions*/
+ sigaction(SIGBUS,&sig,0);
+ sigaction(SIGSEGV,&sig,0);
+ sigaction(SIGFPE,&sig,0);
}
void initthread(struct ___Thread___ * ___this___) {
extern pthread_cond_t gccond;
extern pthread_mutex_t objlock;
extern pthread_cond_t objcond;
+extern pthread_key_t threadlocks;
void initthread(struct ___Thread___ * ___this___);
+
+struct locklist {
+ struct locklist * next;
+ struct locklist * prev;
+ struct ___Object___ * object;
+};
#endif