Browse Source

ttetwasd

master
EliasK 2 years ago
parent
commit
cba34a25e7
  1. 2
      crychat/client/Main.java
  2. 4
      crychat/server/net/ChatServer.java
  3. 39
      crychat/server/util/Queue.java
  4. BIN
      out/production/CryChat/client/Main.class
  5. BIN
      out/production/CryChat/server/net/ChatServer.class
  6. BIN
      out/production/CryChat/server/util/Queue.class

2
crychat/client/Main.java

@ -13,7 +13,7 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Client!"); System.out.println("Client!");
console = new EventLogger(System.out); console = new EventLogger(System.out);
Client c = new Client("localhost", 5001);
Client c = new Client("localhost", 5000);
BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
while (true) { while (true) {
try { try {

4
crychat/server/net/ChatServer.java

@ -21,6 +21,8 @@ public class ChatServer {
Main.console.info("Initializing server on port: " + port + " with max_users: " + max_users); Main.console.info("Initializing server on port: " + port + " with max_users: " + max_users);
this.port = port; this.port = port;
n_users = 0; n_users = 0;
id_q = new Queue<>(max_users);
for (int i = 0; i < max_users; ++i) id_q.enq(i);
t = new Thread(() -> { t = new Thread(() -> {
try { try {
while (true) { while (true) {
@ -37,8 +39,6 @@ public class ChatServer {
e.printStackTrace(); e.printStackTrace();
} }
}); });
id_q = new Queue<>(max_users);
for (int i = 0; i < max_users; ++i) id_q.enq(i);
} }
public void start() { public void start() {

39
crychat/server/util/Queue.java

@ -6,30 +6,53 @@ package server.util;
trying to implement a wait-free queue trying to implement a wait-free queue
*/ */
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicMarkableReference;
import java.util.concurrent.atomic.AtomicReferenceArray; import java.util.concurrent.atomic.AtomicReferenceArray;
public class Queue<T> { public class Queue<T> {
private volatile int head = 0, tail = 0;
private AtomicMarkableReference<Integer> head, tail;
private final int capacity; private final int capacity;
private AtomicReferenceArray<T> items; private AtomicReferenceArray<T> items;
public Queue(int capacity) { public Queue(int capacity) {
this.capacity = capacity; this.capacity = capacity;
items = new AtomicReferenceArray<>(capacity);
this.head = new AtomicMarkableReference(0, false);
this.tail = new AtomicMarkableReference(0, false);
items = new AtomicReferenceArray<T>(capacity);
} }
public boolean enq(T x) { public boolean enq(T x) {
if (tail - head == capacity) return false;
items.set((tail + 1) % capacity, x);
tail++;
boolean[] mark = new boolean[1];
if (tail.get(mark) - head.get(mark) == capacity) return false;
int tmp;
do {
tmp = tail.get(mark);
System.out.println("enq: " + x);
System.out.println("val tmp: " + tmp);
System.out.println("val tmp: " + next(tmp));
items.compareAndSet(tmp, null, x);
} while (tail.compareAndSet(tmp, next(tmp), true, false));
System.out.println("head: " + head.getReference() + "\ntail: " + tail.getReference() + "\ndiff: " + (tail.getReference() - head.getReference()));
return true; return true;
} }
public T deq() { public T deq() {
if (tail - head == 0) return null;
T x = items.get((head + 1) % capacity);
head++;
boolean[] mark = new boolean[1];
System.out.println("test: " + (tail.get(mark) - head.get(mark)) + "\ncap: " + capacity);
if (tail.get(mark) - head.get(mark) == 0) return null;
int tmp;
T x;
do {
tmp = head.get(mark);
System.out.println("head: " + next(tmp));
x = items.getAndSet(tmp, null);
} while (head.compareAndSet(tmp, next(tmp), false, true));
return x; return x;
} }
private int next(int x) {
return (x + 1) % capacity;
}
} }

BIN
out/production/CryChat/client/Main.class

BIN
out/production/CryChat/server/net/ChatServer.class

BIN
out/production/CryChat/server/util/Queue.class

Loading…
Cancel
Save