<commit_before>package ddtxn

import (
	"ddtxn/wfmutex"
	"flag"
	"log"
	"sync"
	"sync/atomic"
)

var Conflicts = flag.Bool("conflicts", false, "Measure conflicts\n")

type KeyType int

const (
	SUM = iota
	MAX
	WRITE
	LIST
	ORDERED_PAIR
)

type BRecord struct {
	padding   [128]byte
	key       Key
	key_type  KeyType
	dd        bool
	last      wfmutex.WFMutex
	value     Value
	int_value int32
	entries   []Entry
	mu        sync.RWMutex
	conflict  int32 // how many times was the lock already held when someone wanted it
	padding1  [128]byte
}

func MakeBR(k Key, val Value, kt KeyType) *BRecord {
	//dlog.Printf("Making %v %v %v\n", k, val, kt)
	b := &BRecord{
		key:      k,
		last:     wfmutex.WFMutex{},
		key_type: kt,
	}
	switch kt {
	case SUM:
		if val != nil {
			b.int_value = val.(int32)
		}
	case MAX:
		if val != nil {
			b.int_value = val.(int32)
		}
	case WRITE:
		if val != nil {
			b.value = val
		}
	case LIST:
		if val == nil {
			b.entries = make([]Entry, 0)
		} else {
			b.entries = make([]Entry, 1)
			b.entries[0] = val.(Entry)
		}
	case ORDERED_PAIR:
		return nil
	}
	return b
}

func (br *BRecord) SLock() {
	br.mu.Lock()
}

func (br *BRecord) SUnlock() {
	br.mu.Unlock()
}

func (br *BRecord) SRLock() {
	br.mu.RLock()
}

func (br *BRecord) SRUnlock() {
	br.mu.RUnlock()
}

func (br *BRecord) Value() Value {
	switch br.key_type {
	case SUM:
		return br.int_value
	case MAX:
		return br.int_value
	case WRITE:
		return br.value
	case LIST:
		return br.entries
	}
	return nil<commit_msg>use my own spinlocks<commit_after>package ddtxn

import (
	"ddtxn/spinlock"
	"ddtxn/wfmutex"
	"flag"
	"log"
	"sync"
	"sync/atomic"
)

var Conflicts = flag.Bool("conflicts", false, "Measure conflicts\n")

var Spinlock = flag.Bool("spinlock", true, "Use spinlocks for 2PL\n")

type KeyType int

const (
	SUM = iota
	MAX
	WRITE
	LIST
	ORDERED_PAIR
)

type BRecord struct {
	padding   [128]byte
	key       Key
	key_type  KeyType
	dd        bool
	last      wfmutex.WFMutex
	lock      spinlock.RWSpinlock
	value     Value
	int_value int32
	entries   []Entry
	mu        sync.RWMutex
	conflict  int32 // how many times was the lock already held when someone wanted it
	padding1  [128]byte
}

func MakeBR(k Key, val Value, kt KeyType) *BRecord {
	//dlog.Printf("Making %v %v %v\n", k, val, kt)
	b := &BRecord{
		key:      k,
		last:     wfmutex.WFMutex{},
		key_type: kt,
	}
	switch kt {
	case SUM:
		if val != nil {
			b.int_value = val.(int32)
		}
	case MAX:
		if val != nil {
			b.int_value = val.(int32)
		}
	case WRITE:
		if val != nil {
			b.value = val
		}
	case LIST:
		if val == nil {
			b.entries = make([]Entry, 0)
		} else {
			b.entries = make([]Entry, 1)
			b.entries[0] = val.(Entry)
		}
	case ORDERED_PAIR:
		return nil
	}
	return b
}

func (br *BRecord) SLock() {
	if *Spinlock {
		br.lock.Lock()
	} else {
		br.mu.Lock()
	}
}

func (br *BRecord) SUnlock() {
	if *Spinlock {
		br.lock.Unlock()
	} else {
		br.mu.Unlock()
	}
}

func (br *BRecord) SRLock() {
	if *Spinlock {
		br.lock.RLock()
	} else {
		br.mu.RLock()
	}
}

func (br *BRecord) SRUnlock() {
	if *Spinlock {
		br.lock.RUnlock()
	} else {
		br.mu.RUnlock()
	}
}

func (br *BRecord) Value() Value {
	switch br.key_type {
	case SUM:
		return br.int_value
	case MAX:
		return br.int_value
	case WRITE:
		return br.value
	case LIST:
		return br.entries
	}
	return nil
{'fraction_non_alphanumeric': 0.13303437967115098, 'fraction_numerical': 0.011061285500747383, 'mean_word_length': 2.7260579064587973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6452608', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1403, 'n_words': 373}