#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

volatile long flag = 0;

long test_and_set(volatile long* lock)
{
        int old;
        asm("xchgl %0, %1"
            : "=r"(old), "=m"(*lock)
            : "0"(1)
            : "memory"
            );
        return old;
}

void lock() 
{
        while(test_and_set(&flag))
                ;
}

void unlock() 
{
        flag = 0;
}

int balance = 1000;

void* deposit(void *arg)
{
        int i;
        for(i=0; i<1e7; ++i) {
                lock();
                ++balance;
                unlock();
        }
        return NULL;
}

void* withdraw(void *arg)
{
        int i;
        for(i=0; i<1e7; ++i) {
                lock();
                --balance;
                unlock();
        }
        return NULL;
}

int main()
{
        pthread_t t1, t2;

        pthread_create(&t1, NULL, deposit, (void*)1);
        pthread_create(&t2, NULL, withdraw, (void*)2);

        pthread_join(t1, NULL);
        pthread_join(t2, NULL);

        printf("ending balance = %d\n", balance);
        return 0;
}
