Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Added check for negative nanosTimeout after connection acquisition. I… #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*~
target/
pom.xml.tag
pom.xml.releaseBackup
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ The MySQLSleepBasedCondition is based on the MySQL ``SLEEP()`` and ``KILL QUERY`

The thread that is woken up is guaranteed to be the one that has waited the longest.

TODO: Try other MySQL features for locking that may perform better than SLEEP():
- http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock
- https://dev.mysql.com/doc/internals/en/debug-sync-facility.html
- https://dev.mysql.com/doc/internals/en/wait-condition.html


Queue
-----
Expand Down
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
<goals>
<goal>jar</goal>
</goals>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
319 changes: 163 additions & 156 deletions src/main/java/net/bramp/concurrent/ConcurrentBitSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,168 +6,175 @@

/**
* Simple wrapper around BitSet to make it thread safe
*
* @author bramp
*
*/
public class ConcurrentBitSet {
final BitSet set;

public ConcurrentBitSet() {
set = new BitSet();
}

public ConcurrentBitSet(int nbits) {
set = new BitSet(nbits);
}

/**
* Atomically sets the value to the given updated value if the current value == the expected value.
*
* @param bitIndex
* @param expect
* @param update
* @return true if successful. False return indicates that the actual value was not equal to the expected value.
*/
public synchronized boolean compareAndSet(int bitIndex, boolean expect, boolean update) {
if (get(bitIndex) == expect) {
set(bitIndex, update);
return true;
}
return false;
}

/**
* Return a set of clear indexes
* @return
*/
public Set<Integer> getClearBits(int max) {
Set<Integer> missing = new TreeSet<Integer>();
int nextBit = -1;
while (true) {
nextBit = this.nextClearBit(nextBit + 1);
if (nextBit >= max)
break;
missing.add(nextBit);
}
return missing;
}

public synchronized byte[] toByteArray() {
return set.toByteArray();
}

public synchronized long[] toLongArray() {
return set.toLongArray();
}

public synchronized void flip(int bitIndex) {
set.flip(bitIndex);
}

public synchronized void flip(int fromIndex, int toIndex) {
set.flip(fromIndex, toIndex);
}

public synchronized void set(int bitIndex) {
set.set(bitIndex);
}

public synchronized void set(int bitIndex, boolean value) {
set.set(bitIndex, value);
}

public synchronized void set(int fromIndex, int toIndex) {
set.set(fromIndex, toIndex);
}

public synchronized void set(int fromIndex, int toIndex, boolean value) {
set.set(fromIndex, toIndex, value);
}

public synchronized void clear(int bitIndex) {
set.clear(bitIndex);
}

public synchronized void clear(int fromIndex, int toIndex) {
set.clear(fromIndex, toIndex);
}

public synchronized void clear() {
set.clear();
}

public synchronized boolean get(int bitIndex) {
return set.get(bitIndex);
}

public synchronized BitSet get(int fromIndex, int toIndex) {
return set.get(fromIndex, toIndex);
}

public synchronized int nextSetBit(int fromIndex) {
return set.nextSetBit(fromIndex);
}

public synchronized int nextClearBit(int fromIndex) {
return set.nextClearBit(fromIndex);
}

public synchronized int previousSetBit(int fromIndex) {
return set.previousSetBit(fromIndex);
}

public synchronized int previousClearBit(int fromIndex) {
return set.previousClearBit(fromIndex);
}

public synchronized int length() {
return set.length();
}

public synchronized boolean isEmpty() {
return set.isEmpty();
}

public synchronized boolean intersects(BitSet set) {
return set.intersects(set);
}

public synchronized int cardinality() {
return set.cardinality();
}

public synchronized void and(BitSet set) {
set.and(set);
}

public synchronized void or(BitSet set) {
set.or(set);
}

public synchronized void xor(BitSet set) {
set.xor(set);
}

public synchronized void andNot(BitSet set) {
set.andNot(set);
}

public synchronized int hashCode() {
return set.hashCode();
}
final BitSet set;

public ConcurrentBitSet() {
set = new BitSet();
}

public ConcurrentBitSet(int nbits) {
set = new BitSet(nbits);
}

/**
* Atomically sets the value to the given updated value if the current value
* == the expected value.
*
* @param bitIndex
* @param expect
* @param update
* @return true if successful. False return indicates that the actual value
* was not equal to the expected value.
*/
public synchronized boolean compareAndSet(int bitIndex, boolean expect, boolean update) {
if (get(bitIndex) == expect) {
set(bitIndex, update);
return true;
}
return false;
}

/**
* Return a set of clear indexes
*
* @return
*/
public Set<Integer> getClearBits(int max) {
Set<Integer> missing = new TreeSet<Integer>();
int nextBit = -1;
while (true) {
nextBit = this.nextClearBit(nextBit + 1);
if (nextBit >= max) {
break;
}
missing.add(nextBit);
}
return missing;
}

public synchronized byte[] toByteArray() {
return set.toByteArray();
}

public synchronized long[] toLongArray() {
return set.toLongArray();
}

public synchronized void flip(int bitIndex) {
set.flip(bitIndex);
}

public synchronized void flip(int fromIndex, int toIndex) {
set.flip(fromIndex, toIndex);
}

public synchronized void set(int bitIndex) {
set.set(bitIndex);
}

public synchronized void set(int bitIndex, boolean value) {
set.set(bitIndex, value);
}

public synchronized void set(int fromIndex, int toIndex) {
set.set(fromIndex, toIndex);
}

public synchronized void set(int fromIndex, int toIndex, boolean value) {
set.set(fromIndex, toIndex, value);
}

public synchronized void clear(int bitIndex) {
set.clear(bitIndex);
}

public synchronized void clear(int fromIndex, int toIndex) {
set.clear(fromIndex, toIndex);
}

public synchronized void clear() {
set.clear();
}

public synchronized boolean get(int bitIndex) {
return set.get(bitIndex);
}

public synchronized BitSet get(int fromIndex, int toIndex) {
return set.get(fromIndex, toIndex);
}

public synchronized int nextSetBit(int fromIndex) {
return set.nextSetBit(fromIndex);
}

public synchronized int nextClearBit(int fromIndex) {
return set.nextClearBit(fromIndex);
}

public synchronized int previousSetBit(int fromIndex) {
return set.previousSetBit(fromIndex);
}

public synchronized int previousClearBit(int fromIndex) {
return set.previousClearBit(fromIndex);
}

public synchronized int length() {
return set.length();
}

public synchronized boolean isEmpty() {
return set.isEmpty();
}

public synchronized boolean intersects(BitSet set) {
return set.intersects(set);
}

public synchronized int cardinality() {
return set.cardinality();
}

public synchronized void and(BitSet set) {
set.and(set);
}

public synchronized void or(BitSet set) {
set.or(set);
}

public synchronized void xor(BitSet set) {
set.xor(set);
}

public synchronized void andNot(BitSet set) {
set.andNot(set);
}

public synchronized int size() {
return set.size();
}
public synchronized int hashCode() {
return set.hashCode();
}

public synchronized boolean equals(Object obj) {
if (obj instanceof ConcurrentBitSet)
return set.equals( ((ConcurrentBitSet)obj).set );
public synchronized int size() {
return set.size();
}

return false;
}
public synchronized boolean equals(Object obj) {
if (obj instanceof ConcurrentBitSet) {
return set.equals(((ConcurrentBitSet) obj).set);
}

return false;
}

public synchronized String toString() {
return set.toString();
}
}
public synchronized String toString() {
return set.toString();
}
}
Loading